【问题标题】:Android Bitmap bigger than heapSizeAndroid Bitmap 大于 heapSize
【发布时间】:2012-04-13 18:32:55
【问题描述】:

我收到错误消息:java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=12295KB, Allocated=3007KB, Bitmap Size=15621KB)

位图大小比我的 heapSize 大.. 那么我怎样才能使位图更小呢?这是它崩溃的代码:

private Bitmap getPicture(int position) {
    Bitmap bmpOriginal = BitmapFactory.decodeFile(mFileLocations.get(position));
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.RGB_565);
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    bmpOriginal.recycle();
}

它在行崩溃:

 Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.RGB_565);

还有我的 decodeFile:

private Bitmap decodeFile(String f){
    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_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            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) {
        Log.d(LOG_TAG, "Failed to decode file");
    }
    return null;
}

【问题讨论】:

  • 我没有看到你在哪里使用你的 decodeFile 方法。你只是在使用BitmapFactory.decodeFile
  • 哦,哇,我在其他地方使用它,但我想我一定是在尝试的时候改变了它。谢谢

标签: android bitmap out-of-memory


【解决方案1】:

缩小您的映像以包含在 VM 预算中...

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( filename, options );
        options.inJustDecodeBounds = false;
        options.inSampleSize = 4; 

        bitmap = BitmapFactory.decodeFile( filename, options );
        if ( bitmap != null ) {
            bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
        }

将样本大小更改为您想要的任何值,例如 2、4、6、8 等。

有关详细信息,请参阅最近发布的 Android 开发者网站的 this,它清楚地说明了您需要做些什么才能达到 VM 预算。

【讨论】:

    【解决方案2】:

    两件事:

    您使用的似乎是BitmapFactory.decodeFile 方法,而不是您发布的方法。

    此外,您从bmpOriginal 保留的唯一信息似乎是它的尺寸。那么,为什么不像在 decodeFile 方法中那样使用 BitmapFactory.inJustDecodeBounds。或者在创建新位图之前回收位图,同时保留其尺寸,如下所示:

    int width = bmpOriginal.getWidth();
    int height = bmpOriginal.getHeight();
    bmpOriginal.recycle();
    Bitmap bmResult = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    

    【讨论】:

      【解决方案3】:

      AndroidDevelopers+ 最近对此进行了介绍:

      https://plus.google.com/103125970510649691204/posts/1oSFSyv3pRj

      【讨论】:

      • 在 SO 上大约一百万次
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多