位图图像内存不足 [英] Bitmap Image out of memory

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

问题描述

我知道有很多关于内存不足的 android 位图图像的讨论,但我想知道是否有人可以向我解释..

I know there is a lot of discussion about android bitmap images out of memory but I was wondering if someone could explain it to me..

目前在我的应用程序中,我有一个活动,其中列出了图像缩略图(低质量),当我单击图像时,它会打开一个新活动以全屏查看图像.在我的第二个活动课中,我有:

Currently in my app I have an activity which lists image thumbnail (low quality) and when I click an image it opens a new activity to view the image full screen. In my 2nd activity class I have:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bm = BitmapFactory.decodeFile(myImagePath, options);

然后我把它放到一个 ImageView 中来显示它.这有效并以完整质量显示我的图像.但是,如果我单击返回,然后再次单击以查看该图像(并重复此操作 6 次).. 在第 6 次打开图像(活动 2)时,出现内存不足错误,提示 Heap size=6919KB, Allocated=3125KB, Bitmap大小 = 25848KB!

I then put this into an ImageView to display it. This works and displays my image to its full quality. However if i click back and then click to see that image again (and repeat this 6 times) .. on the 6th time opening the image (activity2) I get an out of memory error saying Heap size=6919KB, Allocated=3125KB, Bitmap Size = 25848KB!

位图大小怎么这么大?我认为它可能一直在创建新实例,所以我决定在我的第二个活动中放置一个方法,当按下后退键时..在这个方法中,我设置了我的 bitmap=null 并且还做了 System.gc()清除垃圾收集器但这并没有解决问题.我在第 6 次点击缩略图以全分辨率查看图像时仍然出现内存不足错误

How is bitmap size that big? I assumed it may be creating new instances all the time so I then decided to put a method in my 2nd activity for when the back key is pressed..and in this method I set my bitmap=null and also did System.gc() to clear the garbage collector BUT this did not fix the problem. I still get an out of memory error on the 6th time of clicking on the thumbnail to view the image in full resolution

谁能解释一下为什么?谢谢

Can anyone explain why? Thanks

推荐答案

android 提供了一些很好的信息,详细解释了这一切,以及如何克服这个问题 此处.

There is some great information from android that explains it all in detail, and how to overcome this problem here.

每个像素为 4 字节.600 万像素 = 24MB

Each pixel is 4 Bytes. 6M Pixel = 24MBs

一张照片可以用完所有内存.

One photo can use up all the Memory.

位图占用大量内存,尤其是对于照片等丰富的图像.例如,Galaxy Nexus 上的相机可以拍摄高达 2592x1936 像素(5 兆像素)的照片.如果使用的位图配置是 ARGB_8888(Android 2.3 以后的默认配置),则将此图像加载到内存中需要大约 19MB 内存(2592*1936*4 字节),立即耗尽某些设备上的每个应用程序限制.

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

我再次向您指出这个 很棒的链接我从另一个有教程的 SO 问题中找到的如何正确解决问题.

Again I point you to this great link I found from another SO Question that has tutorials of how to properly over come the problem.

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

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