android - android中的位图内存不足错误...

标签 android memory-management memory-leaks android-imageview

我在 Android 应用程序中工作,我正在使用位图将图像绑定(bind)到 ImageView。我的要求是旋转该 ImageView 并为该 ImageView 提供边框。我已经成功地实现了这个,但是在应用程序使用这个 Activity 两到三次之后,出现了一个“强制关闭”错误,说 Bitmap out of VM memory。请帮助我尽量减少代码中的位图内存消耗。并让我知道如何修改相同的代码?

final int BORDER_WIDTH = 5;
        // Set the border color
        final int BORDER_COLOR = Color.WHITE;
        Bitmap res = Bitmap.createBitmap(CAPTURE_IMAGE.getWidth() + 2
                * BORDER_WIDTH, CAPTURE_IMAGE.getHeight() + 2 * BORDER_WIDTH,
                CAPTURE_IMAGE.getConfig());
        System.gc();
        Canvas canvas = new Canvas(res);
        Paint paint = new Paint();
        paint.setColor(BORDER_COLOR);
        canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint);

        canvas.drawBitmap(CAPTURE_IMAGE, BORDER_WIDTH, BORDER_WIDTH, paint);
        Matrix mat = new Matrix();
        // Set the Imageview position
        mat.postRotate(355);

        bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(),
                res.getHeight(), mat, true);
        System.gc();
        res.recycle();
        res = null;
        paint = null;
        canvas = null;
        mat = null;
        // Set the captured bitmap image in the imageview
        mShareImageView.setImageBitmap(bMapRotate);

最佳答案

我认为你应该像这样使用收缩函数

bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(),
                res.getHeight(), mat, true);


Bitmap myBitmap = ShrinkBitmap(bMapRotate , 300, 300);

mShareImageView.setImageBitmap(myBitmap );


private Bitmap ShrinkBitmap(String file, int width, int height) {
        // TODO Auto-generated method stub
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

        if (heightRatio > 1 || widthRatio > 1)
        {
         if (heightRatio > widthRatio)
         {
          bmpFactoryOptions.inSampleSize = heightRatio;
         } else {
          bmpFactoryOptions.inSampleSize = widthRatio;
         }
        }

        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
     return bitmap;
    }

它对我有用,我避免了 Bitmap out of VM memory 异常

关于android - android中的位图内存不足错误...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608064/

相关文章:

android - 如何使用新的导航架构组件禁用某些 fragment 的导航中的 UP?

安卓 "No view found for id for fragment"

java - Android Air Native Extension 安装新的 apk 版本抛出 "no activity found to handle intent"

javascript - 从 Unresolved promise 会导致内存泄漏吗?

java - 如何调整相机或图库照片的大小

java - 如果 new 失败了怎么办?

objective-c - 使用自动引用计数释放 self 的新模式是什么?

c++ - 我可以在不引发 C++ 异常的情况下检查内存块是否可读吗?

C 内存分配 : Fixed-length array in a struct in a struct

java - for 循环对象引用 while GC