由于XUtils众所周知的缺陷,项目急需将原有的依赖项目XUtils更新为XUtils3。
XUtils3的架构与之前的版本完全不同,但是依旧没有支持drawable。
有了之前的经验,XUtils3改造起来就简单了。
下载XUtils3的源码,全局搜索"assets",找到这个位置
//org.xutils.http.request.UriRequestFactory
public static UriRequest getUriRequest(RequestParams params, Type loadType) throws Throwable {
// get scheme
String scheme = null;
String uri = params.getUri();
int index = uri.indexOf(":");
if (index > 0) {
scheme = uri.substring(0, index);
} else if (uri.startsWith("/")) {
scheme = "file";
}
// get UriRequest
if (!TextUtils.isEmpty(scheme)) {
Class<? extends UriRequest> cls = SCHEME_CLS_MAP.get(scheme);
if (cls != null) {
Constructor<? extends UriRequest> constructor
= cls.getConstructor(RequestParams.class, Class.class);
return constructor.newInstance(params, loadType);
} else {
if (scheme.startsWith("http")) {
return new HttpRequest(params, loadType);
} else if (scheme.equals("assets")) {
return new AssetsRequest(params, loadType);
} else if (scheme.equals("file")) {
return new LocalFileRequest(params, loadType);
} else {
throw new IllegalArgumentException("The url not be support: " + uri);
}
}
} else {
throw new IllegalArgumentException("The url not be support: " + uri);
}
}
这里可以看到,XUtils3与xUtils架构已经不同。
找到AssetsRequest的关键代码
//org.xutils.http.request.AssetsRequest
@Override
public InputStream getInputStream() throws IOException {
if (inputStream == null) {
if (callingClassLoader != null) {
String assetsPath = "assets/" + queryUrl.substring("assets://".length());
inputStream = callingClassLoader.getResourceAsStream(assetsPath);
contentLength = inputStream.available();
}
}
return inputStream;
}
使用ClassLoader.getResourceAsStream()读取本地资源文件,这貌似不适用drawable中的资源,我们需要的是上下文,xUtils3控制中心已经为我们准备好了。
x.app()可以获得应用实例,老方法Context.getResources().openRawResource()读取资源
我们是代码的搬运工。复制AssetsRequest生成DrawableRequest,改造getInputStream()方法
//org.xutils.http.request.DrawableRequest
@Override
public InputStream getInputStream() throws IOException {
if (inputStream == null) {
if (callingClassLoader != null) {
String drawableIdString = queryUrl.substring(11, queryUrl.length());
try {
int drawableId = Integer.parseInt(drawableIdString);
inputStream = x.app().getResources().openRawResource(drawableId);
contentLength = inputStream.available();
}catch (Exception e){
e.printStackTrace();
}
}
}
return inputStream;
}
改造UriRequestFactory的getUriRequest方法,加入构建DrawableRequest的代码
//org.xutils.http.request.UriRequestFactory
public static UriRequest getUriRequest(RequestParams params, Type loadType) throws Throwable {
// get scheme
String scheme = null;
String uri = params.getUri();
int index = uri.indexOf(":");
if (index > 0) {
scheme = uri.substring(0, index);
} else if (uri.startsWith("/")) {
scheme = "file";
}
// get UriRequest
if (!TextUtils.isEmpty(scheme)) {
Class<? extends UriRequest> cls = SCHEME_CLS_MAP.get(scheme);
if (cls != null) {
Constructor<? extends UriRequest> constructor
= cls.getConstructor(RequestParams.class, Class.class);
return constructor.newInstance(params, loadType);
} else {
if (scheme.startsWith("http")) {
return new HttpRequest(params, loadType);
} else if (scheme.equals("assets")) {
return new AssetsRequest(params, loadType);
} else if (scheme.equals("file")) {
return new LocalFileRequest(params, loadType);
} else if (scheme.equals("drawable")) {
return new DrawableRequest(params, loadType);
} else {
throw new IllegalArgumentException("The url not be support: " + uri);
}
}
} else {
throw new IllegalArgumentException("The url not be support: " + uri);
}
}
至此,改造完成。使用方法与xutils相同。
String uri = "drawable://" +R.drawable.super_larger_logo
x.image().bind(largePic_imgV, uri);
附改造后的xUtils3源码http://download.csdn.net/detail/atm008/9477902