如何避免内存不足与创建位图问题 [英] how to avoid OUtOfMemory problems with create Bitmap

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

问题描述

我想从这个code视图创建位图:

 公共位图getmyBitmap(视图V)
{
        位图B = Bitmap.createBitmap(v.getWidth(),v.getHeight(),
        Bitmap.Config.ARGB_8888);
        帆布C =新的Canvas(B);
        v.draw(C);
        返回b;
}

不过,我有一个内存不足的问题。我可以通过添加该选项可将清单文件解决它的的android:largeHeap =真正的不推荐!

我在想视图的循环,可能是一个解决办法?

这是printStack:


  

15 11-25:31:46.556 2115年至2115年/ com.myproject.android E / dalvikvm堆:
  出于对4096016字节分配内存。 11-25 15:31:46.616结果
  2115年至2115年/ com.myproject.android E / dalvikvm堆:内存上
  4096016字节的分配。 11-25 15:31:46.666结果
  2115年至2115年/ com.myproject.android E / dalvikvm堆:内存上
  4096016字节的分配。 11-25 15:31:54.016结果
  2115年至2115年/ com.myproject.android E / dalvikvm堆:内存上
  1879696字节的分配。 11-25 15:31:54.016结果
  2115年至2115年/ com.myproject.android E / AndroidRuntime:致命异常:
  主



解决方案

我觉得你得到了很多位图的其中超载您的系统内存,或者如果它一个一个认为这是一个非常巨大的,所以,解决这个问题,你必须做两件事情,第一个确保你不运行这个方法很多次同样的位图(如导致存储在你的记忆和所有属于同其中之一的大量位图),第二使用自定义的方法来扩展您的位图,以降低它的大小,因此它的内存占用面积

  //扩展你的位图
//分配newWidth和newHeight与相应的宽度和高度,不会让你的记忆超载和在同一时间不会使你的形象失去了它的功能
公共静态位图scaleBitmap(位图bitmapToScale,浮newWidth,浮newHeight){    如果(bitmapToScale == NULL)
        返回null;
    //获取原始宽度和高度
    INT宽度= bitmapToScale.getWidth();
    INT高度= bitmapToScale.getHeight();
    //创建用于操作一个矩阵
    字模=新的Matrix();    //调整位图
    matrix.postScale(newWidth /宽,newHeight /高);    //重新创建新的位图,将其设置回
    返回Bitmap.createBitmap(bitmapToScale,0,0,bitmapToScale.getWidth(),bitmapToScale.getHeight(),矩阵,真);}

I'm trying to create a bitmap from a view with this code :

public Bitmap getmyBitmap(View v)
{
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
        Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
}

But I have an Out Of Memory problems. I can fix it by adding this option to the Manifest file android:largeHeap="true" which is not recommended !!

I'm thinking about the recycle of the view ,could be a solution?

this is the printStack:

11-25 15:31:46.556 2115-2115/com.myproject.android E/dalvikvm-heap﹕ Out of memory on a 4096016-byte allocation. 11-25 15:31:46.616
2115-2115/com.myproject.android E/dalvikvm-heap﹕ Out of memory on a 4096016-byte allocation. 11-25 15:31:46.666
2115-2115/com.myproject.android E/dalvikvm-heap﹕ Out of memory on a 4096016-byte allocation. 11-25 15:31:54.016
2115-2115/com.myproject.android E/dalvikvm-heap﹕ Out of memory on a 1879696-byte allocation. 11-25 15:31:54.016
2115-2115/com.myproject.android E/AndroidRuntime﹕ FATAL EXCEPTION: main

解决方案

I think you are getting a lot of Bitmaps which overloads your system memory, or if it a single one a think it's an extremely huge one, So, to solve the problem, you have to do two things, first make sure you don't run this method a lot of times for the same Bitmap(as that leads to a lot of Bitmaps stored in your memory and all of them belonging to the same one), second use a custom method to scale your Bitmaps in order to lower it's size and hence it's memory occupation area:

// to scale your Bitmaps
// assign newWidth and newHeight with the corresponding width and height that doesn't make your memory overloads and in the same time doesn't make your image loses it's Features
public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {

    if(bitmapToScale == null)
        return null;
    //get the original width and height
    int width = bitmapToScale.getWidth();
    int height = bitmapToScale.getHeight();
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(newWidth / width, newHeight / height);

    // recreate the new Bitmap and set it back
    return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);  

}

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

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