从assets中加载多个图片出错,求高人解决,急!

cddhy 2012-01-04 10:51:46

public static Bitmap getImageFromAssetsFile(Resources res,String fileName)
{
Bitmap image = null;
AssetManager am = res.getAssets();
InputStream is = null;
try
{
is=am.open(fileName);
image = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return image;
}



这是我加载图片的代码。
我一次加载了几十张图片,就报

01-04 01:50:12.964: ERROR/AndroidRuntime(325): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

网上搜了很多方法都得不到解决。

求解决办法或思路。
求优化办法或思路。
...全文
250 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjkto 2012-01-06
  • 打赏
  • 举报
回复

private void dispalyNextImage() {
// 如果发生数组越界
if (currentImg >= images.length) {
currentImg = 0;
}
//备注1
// 找到下一个图片文件
while (!images[currentImg].endsWith(".png")
&& !images[currentImg].endsWith(".jpg")
&& !images[currentImg].endsWith(".gif")) {
currentImg++;
// 如果已发生数组越界
if (currentImg >= images.length) {
currentImg = 0;
}
}

InputStream assetFile = null;
try {
// 打开指定资源对应的输入流
assetFile = assets.open(images[currentImg++]);
} catch (IOException e) {
e.printStackTrace();
}

BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
//备注2
// 如果图片还未回收,先强制回收该图片
if (bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
// 改变ImageView显示的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
}

提供代码吧,更直接一些!!
hjkto 2012-01-06
  • 打赏
  • 举报
回复
我也加载不少,没报错,你加载的数量和大小是多少?
CyberLogix 2012-01-06
  • 打赏
  • 举报
回复
应该是没有释放资源导致的内存不足,:-)
念茜 2012-01-06
  • 打赏
  • 举报
回复
这个问题都不知道说了多少遍了,其实还是需要具体情况具体分析。
LZ Google一下java.lang.OutOfMemoryError,会收获很多,还是要自己实践的。
rlini 2012-01-06
  • 打赏
  • 举报
回复
2L说的正确的呀。。LZ不明白?
cddhy 2012-01-04
  • 打赏
  • 举报
回复
加载少量的图片没问题。图片量大了就报错了。
贪睡的兔子 2012-01-04
  • 打赏
  • 举报
回复
图片资源用完回收掉吧 bmp.recycle()
lyy900907 2012-01-04
  • 打赏
  • 举报
回复
wow,我还是回去再学学吧
cddhy 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hswujingjing 的回复:]

资源管理,你只加载不清除,再多的内存也被你用光啊,你用到的时候加载进来,用完就recycle()掉。
[/Quote]
gc回收吗?还是别的?
丿渎灬魂 2012-01-04
  • 打赏
  • 举报
回复
内存溢出。
超过8M就会报错
hswujingjing 2012-01-04
  • 打赏
  • 举报
回复
资源管理,你只加载不清除,再多的内存也被你用光啊,你用到的时候加载进来,用完就recycle()掉。
cddhy 2012-01-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wensefu 的回复:]

这个问题是所有做android app都会碰到而且必须学会解决的问题 网上很多 自己找找吧 oom
[/Quote]

呃。。。
能给点提示不?
wensefu 2012-01-04
  • 打赏
  • 举报
回复
这个问题是所有做android app都会碰到而且必须学会解决的问题 网上很多 自己找找吧 oom

80,351

社区成员

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

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