Android的位图的OutOfMemoryError [英] Android Bitmap OutOfMemoryError

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

问题描述

我有我的VSD220一个OutOfMemoryError(这是一个22基于Android的多合一)

 为(ImageView的IMG:listImages){
            System.gc()的;            位图MYBITMAP = BitmapFactory.de codeFILE(路径);
            img.setImageBitmap(MYBITMAP);
            img.setOnClickListener(本);
        }

我真的不知道该怎么办,因为这个形象是最大分辨率之下。图像尺寸为一些关于(1000×1000),显示这是1920×1080。

任何帮助吗?
(这的foreach循环进行约20个元素,它GOTS 6或7圈后破裂。)

非常感谢。

埃塞基耶尔。


解决方案

您应该看一看为的管理位图内存。根据您的操作系统版本,可以使用不同的技术,让您可以管理更多的位图,但你可能会无论如何都要改变你的code。

在特定的,你很可能将不得不在使用code的修订版的加载一个缩小版本到内存,但我至少发现这部分是特别有用的:

 公共静态INT calculateInSampleSize(
            BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
    //原始高度和图像宽度
    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;
    INT inSampleSize = 1;    如果(高度> reqHeight ||宽度GT; reqWidth){        //计算的高度和宽度的比率要求的高度和宽度
        最终诠释heightRatio = Math.round((浮点)高度/(浮点)reqHeight);
        最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);        //选择最小比率inSampleSize值,这将保证
        //使用两个维度大于或等于所述最终图像
        //请求的高度和宽度。
        inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
    }    返回inSampleSize;
}公共静态位图德codeSampledBitmapFromResource(资源RES,INT渣油,
        INT reqWidth,诠释reqHeight){    //首先去code。与inJustDe codeBounds = true来检查尺寸
    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeResource(RES,渣油,期权);    //计算inSampleSize
    options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);    //德code位与inSampleSize集
    options.inJustDe codeBounds = FALSE;
    返回BitmapFactory.de codeResource(RES,渣油,期权);
}


  

这个方法可以很容易地加载任意大尺寸的位图
  成一个显示100×100像素的缩略图,如图一个ImageView的
  下面的例子code:


  mImageView.setImageBitmap(
德codeSampledBitmapFromResource(getResources(),R.id.myimage,100,100));

I'm having an OutOfMemoryError in my VSD220 (It's a 22" Android based All in one)

for (ImageView img : listImages) {
            System.gc();

            Bitmap myBitmap = BitmapFactory.decodeFile(path);
            img.setImageBitmap(myBitmap);
            img.setOnClickListener(this);
        }

I really don't know what to do, because this image is below the maximum resolution. The image size is something about (1000x1000), and the display it's 1920x1080.

Any help? (That foreach cycle is for about 20 elements, it gots broken after 6, or 7 loops..)

Thanks a lot.

Ezequiel.

解决方案

You should take a look at the training docs for Managing Bitmap Memory. Depending on your OS version, you could use different techniques to allow you to manage more Bitmaps, but you'll probably have to change your code anyway.

In particular, you're probably going to have to use an amended version of the code in "Load a Scaled Down Version into Memory", but I at least have found this section to be particularly useful:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}



public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

This method makes it easy to load a bitmap of arbitrarily large size into an ImageView that displays a 100x100 pixel thumbnail, as shown in the following example code:

mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

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

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