位图内存不足错误 [英] Out of Memory error with Bitmap

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

问题描述

在运行时,我试图在表面视图中放置一个图像.当我尝试使用 Drawable 文件夹中的图像时,出现内存不足错误.在stackoverflow中快速搜索后,我发现如果我们从资产文件夹中访问图像会有所缓解.但我仍然在运行时收到内存不足错误.

During runtime, I am trying to put an image in the surface view. When I tried using the image from the Drawable folder I got Out of memory error. After a quick search in the stackoverflow, I found that there will be some relief if we access the image from the asset folder. But still I get the Out of memory error during runtime.

我分析并发现缩放将有助于解决此类内存相关问题.问题是我的图像大小为 1280 x 720,设备大小也相同.因此我觉得缩放不会有任何影响.

I have analyzed and found that scaling will help in resolving this kind of memory related issues. The thing is that I have the image size of 1280 x 720 and the device size also the same. Hence I feel like the scaling will not have any effect.

由于我们在这个社区中有专家,如果您能帮助我提供一些建议/示例来解决此类问题,我将不胜感激.

As we have experts in this community, I would appreciate if you can help me with some suggestions/examples to resolve this kind of issue.

使用 Drawable 文件夹中的位图.

Using the Bitmap from Drawable folder.

backgoundImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.backgroundhomepage), (int) dWidth, (int) dHeight, true);

    /***********************************************************************************************************************************************************
    1.  To get the image from asset library
     **************************************************************************************************************************************************************/ 

    public  Bitmap getAssetImage(Context context, String filename) throws IOException {
        AssetManager assets = context.getResources().getAssets();
        InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
        Bitmap bitmap = BitmapFactory.decodeStream(buffer);
        return bitmap;
    }

场景 2:

使用资产文件夹中的位图

Scenario 2:

Using the Bitmap from Assets folder

backgoundImage = Bitmap.createScaledBitmap(getAssetImage(context,"backgroundhomepage"), (int) dWidth, (int) dHeight, true);

推荐答案

OutofMemory 当您的应用程序超出堆中分配的内存时发生.位图太大而无法放入内存(即堆)中.在这种情况下,您的内存不足.您需要缩小位图,然后使用相同的.为此,请检查下面的链接

OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap. In such a case you run out of memory. You need to scale down the bitmap and then use the same. For that check the link below

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

还有一个博客@http://android-developer.blogspot.in/2009/01/avoiding-memory-leaks.html(避免内存泄漏)

There is also a blog @ http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html (avoiding memory leaks)

 public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
 try {
     //Decode image size
     BitmapFactory.Options o = new BitmapFactory.Options();
     o.inJustDecodeBounds = true;
     BitmapFactory.decodeStream(new FileInputStream(f),null,o);

     //The new size we want to scale to
     final int REQUIRED_WIDTH=WIDTH;
     final int REQUIRED_HIGHT=HIGHT;
     //Find the correct scale value. It should be the power of 2.
     int scale=1;
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
         scale*=2;

     //Decode with inSampleSize
     BitmapFactory.Options o2 = new BitmapFactory.Options();
     o2.inSampleSize=scale;
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
 } catch (FileNotFoundException e) {}
 return null;
}

引用文档

BitmapFactory 类提供了多种解码方法(decodeByteArray()、decodeFile()、decodeResource() 等),用于从各种来源创建位图.根据您的图像数据源选择最合适的解码方法.这些方法尝试为构造的位图分配内存,因此很容易导致 OutOfMemory 异常.每种类型的解码方法都有额外的签名,让您可以通过 BitmapFactory.Options 类指定解码选项.

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.

在解码时将 inJustDecodeBounds 属性设置为 true 可避免内存分配,位图对象返回 null 但设置 outWidth、outHeight 和 outMimeType.这种技术允许您在位图构建(和内存分配)之前读取图像数据的尺寸和类型.

Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

另请查看此链接以了解内存管理.

Also check this link for memory management.

https://www.youtube.com/watch?v=_CruQY55HOk

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

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