universalimageloader 已经是标配图片加载库了。 不过大部分图片空间都是防盗链的, 自己的图片空间也是。
需要修改源码增加refer参数:
1.下载 source-jar包,解压导入到安卓工程, 没有倚赖包,不会报错。
2.修改下面2个类:都是增加refer部分
3. 把编译出的class文件替换掉jar包中的class即可。
com.nostra13.universalimageloader.core.download.BaseImageDownloader
protected HttpURLConnection createConnection(String url, Object extra) throws IOException {
String encodedUrl = Uri.encode(url, ALLOWED_URI_CHARS);
HttpURLConnection conn = (HttpURLConnection) new URL(encodedUrl).openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
conn.setRequestProperty("Referer", "http://android.xx.cn");
return conn;
}
com.nostra13.universalimageloader.core.download.HttpClientImageDownloader
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
HttpGet httpRequest = new HttpGet(imageUri);
httpRequest.addHeader("Referer", "http://android.xx.cn");
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
return bufHttpEntity.getContent();
}