android加载大图片的问题

fire_fire_fire 2013-05-07 04:03:06
要求不牺牲图片质量的情况下显示大图片,宽高要求均为原图的宽高,我已经临时加大了缓存,虽然不会产生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

Drawable cacheImage = 调用上面的代码得到的drawable
if (cacheImage != null) {
viewHolder.iv_img.setImageDrawable(cacheImage);
}
...全文
146 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangjunbao0818 2013-11-29
  • 打赏
  • 举报
回复
lz弄出来了吗??我也碰到了类似的问题
南瓜饼 2013-05-08
  • 打赏
  • 举报
回复
引用 6 楼 fire_fire_fire 的回复:
[quote=引用 3 楼 wenwei19861106 的回复:] 用软引用 还有不要用drawable 用bitmap
我试过了,不行,不管是bitmap还是drawable都显示不出图片。[/quote]纳就只能用图片压缩的方式了 如果对图片的清晰度要求不高的话
fire_fire_fire 2013-05-08
  • 打赏
  • 举报
回复
引用 3 楼 wenwei19861106 的回复:
用软引用 还有不要用drawable 用bitmap
我试过了,不行,不管是bitmap还是drawable都显示不出图片。
南瓜饼 2013-05-08
  • 打赏
  • 举报
回复
引用 4 楼 fire_fire_fire 的回复:
[quote=引用 3 楼 wenwei19861106 的回复:] 用软引用 还有不要用drawable 用bitmap
1.我用的是软引用 2.我用drawable现在出现的问题是图片的占位出来了,就是占位的地方不显示图片的内容,是个透明的东西。用bitmap可以避免这样的情况吗?为什么呢?谢谢[/quote]bitmap能节省一部分内存的 因为drawable最终也是通过转化成bitmap的形式显示的
fire_fire_fire 2013-05-08
  • 打赏
  • 举报
回复
引用 3 楼 wenwei19861106 的回复:
用软引用 还有不要用drawable 用bitmap
1.我用的是软引用 2.我用drawable现在出现的问题是图片的占位出来了,就是占位的地方不显示图片的内容,是个透明的东西。用bitmap可以避免这样的情况吗?为什么呢?谢谢
南瓜饼 2013-05-08
  • 打赏
  • 举报
回复
用软引用 还有不要用drawable 用bitmap
fire_fire_fire 2013-05-08
  • 打赏
  • 举报
回复
自己顶一个,求解,谢谢,有答案马上结贴
fire_fire_fire 2013-05-08
  • 打赏
  • 举报
回复
引用 7 楼 wenwei19861106 的回复:
[quote=引用 6 楼 fire_fire_fire 的回复:] [quote=引用 3 楼 wenwei19861106 的回复:] 用软引用 还有不要用drawable 用bitmap
我试过了,不行,不管是bitmap还是drawable都显示不出图片。[/quote]纳就只能用图片压缩的方式了 如果对图片的清晰度要求不高的话[/quote] 就是对图片要求高,没办法。我只能在后台上传的时候来限制图片大小了
knxw0001 2013-05-07
  • 打赏
  • 举报
回复
无解

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧