做项目遇到一个问题,服务器给了一些固定活动图片的地址,但是在不同的节假日,又是需要替换的,大家都知道Imageloao加载缓存的图片,如果地址一致,会一直加载缓存的图片,所以就在图片后面拼接一串随机数,这样就可以随时替换图片,而又不需要重新给地址了(忘记说了,我做的是H5混合项目),有些东西是原生的东西,不变的Tab,所以为了体验,减少对服务器的请求,把地址写死了,我也是在网上找了好多,貌似没有这样的回答,所以只能自己看源码了- -
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
String uri = URLEncoder.encode(imageUri, "UTF-8");
uri = uri.replaceAll("\\+", "%20").replaceAll("%3A", ":").replaceAll("%2F", "/");
HttpURLConnection conn = this.createConnection(uri, extra);
for(int redirectCount = 0; conn.getResponseCode() / 100 == 3 && redirectCount < 5; ++redirectCount) {
conn = this.createConnection(conn.getHeaderField("Location"), extra);
}
InputStream imageStream;
try {
imageStream = conn.getInputStream();
} catch (IOException var8) {
IoUtils.readAndCloseStream(conn.getErrorStream());
throw var8;
}
return new ContentLengthInputStream(new BufferedInputStream(imageStream, '耀'), conn.getContentLength());
}
源码里只对地址里部分参数解码,?(%3F),=(%3D)都没有做解码,只能自己重写了
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).imageDownloader(new BaseImageDownloader(context){
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
String uri = URLEncoder.encode(imageUri, "UTF-8");
uri = uri.replaceAll("\\+", "%20").replaceAll("%3A", ":").replaceAll("%2F", "/")
.replaceAll("%3F" , "?").replaceAll("%3D" , "=");
HttpURLConnection conn = this.createConnection(uri, extra);
for(int redirectCount = 0; conn.getResponseCode() / 100 == 3 && redirectCount < 5; ++redirectCount) {
conn = this.createConnection(conn.getHeaderField("Location"), extra);
}
InputStream imageStream;
try {
imageStream = conn.getInputStream();
} catch (IOException var8) {
IoUtils.readAndCloseStream(conn.getErrorStream());
throw var8;
}
return new ContentLengthInputStream(new BufferedInputStream(imageStream, '耀'), conn.getContentLength());
}
})
.threadPriority(Thread.MAX_PRIORITY).denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
.memoryCacheExtraOptions(160, 160).discCacheSize(1024 * 1024 * 200)
.discCacheExtraOptions(160, 160, CompressFormat.PNG, 100, null).build();
ImageLoader.getInstance().init(config);
这个是Imageload初始化的时候的一些设置,重写BaseImageDownloader这个方法就好了,就是这么简单,哈哈,希望能帮到大家!