内存不足异常加载从外部存储位图时, [英] OutOfMemory exception when loading bitmap from external storage

查看:121
本文介绍了内存不足异常加载从外部存储位图时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序加载一对夫妇从JPEG和PNG文件的图像。当我把所有这些文件到资产目录,并加载它以这种方式,一切都很好:

In my application I load a couple of images from JPEG and PNG files. When I place all those files into assets directory and load it in this way, everything is ok:

InputStream stream = getAssets().open(path);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
stream.close();
return new BitmapDrawable(bitmap);

但是,当我尝试从SD卡中加载完全相同的图像,我得到一个内存不足的异常!

But when I try to load the exact same images from sd card, I get an OutOfMemory exception!

InputStream stream = new FileInputStream("/mnt/sdcard/mydata/" + path);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
stream.close();
return new BitmapDrawable(bitmap);

这是我所得到的日志中:

This is what I get in the log:

11-05 00:53:31.003: ERROR/dalvikvm-heap(13183): 827200-byte external allocation too large for this process.
11-05 00:53:31.003: ERROR/GraphicsJNI(13183): VM won't let us allocate 827200 bytes
...
11-05 00:53:31.053: ERROR/AndroidRuntime(13183): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-05 00:53:31.053: ERROR/AndroidRuntime(13183):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
...

为什么能这样呢?

Why can this happen?

更新:试过这两个真实的设备 - 看来我不能大于12MB的位图到什么叫做外部存储器加载更多的(这不是一个SD卡)

UPDATE: Tried both of these on real device - it seems that I can't load more than 12MB of bitmaps into whatever is called "external memory" (this is not an sd card).

推荐答案

也许没有错,你的API的使用,我想我们所能做的是推断使用AssetManager涉及较少背后的幕后堆分配比打开一个随机从SD卡中的文件。

Probably nothing wrong with your API usage, I guess all we can do is infer that using the AssetManager involves less behind-the-scenes heap allocation than opening a random file from the SD card.

800KB是任何人的书了严重的分配......这无疑是为DECOM pressed图像像素。既然你知道图像的大小,什么深度是什么?如果它32bpp的然后尝试覆盖,使用<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#in$p$pferredConfig"相对=nofollow> 在preferredConfig

800KB is a serious allocation in anybody's book... this will doubtless be for the decompressed image pixels. Given that you know the size of the image, what depth is it? If it's 32bpp then try overriding that using inPreferredConfig.

这篇关于内存不足异常加载从外部存储位图时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆