android加载大图片的问题
要求不牺牲图片质量的情况下显示大图片,宽高要求均为原图的宽高,我已经临时加大了缓存,虽然不会产生OOM错误,但如果图片过大就不会显示。如下:
1.listview里面的子项只有一个imageview
2.加载的是sd卡里面的图片
3.使用的是软引用SoftReference<Drawable> softReference
4.图片如果小于1mb没有问题,大于1mb的话不会抛异常,但是图片显示不出来,代码如下:
把本地图片加载进drawable代码:
File f = new File(locationUrl);
if (f.exists() && f.length() > 0) {
// 文件存在
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inPurgeable = true;
bfOptions.inInputShareable = true;
bfOptions.inTempStorage = new byte[32 * 1024];
FileInputStream fs = null;
try {
fs = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = null;
if (fs != null) {
try {
bmp = BitmapFactory.decodeFileDescriptor(fs.getFD(),null, bfOptions);
BitmapDrawable drawable = new BitmapDrawable(bmp);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
return drawable;
} catch (IOException e) {
e.printStackTrace();
}
}
}
将返回的drawable加载进imageview
Android imageview bmp drawable
Drawable cacheImage = 调用上面的代码得到的drawable
if (cacheImage != null) {
viewHolder.iv_img.setImageDrawable(cacheImage);
}
[解决办法]
用软引用 还有不要用drawable 用bitmap