【问题标题】:Android recycled bitmap errorAndroid回收位图错误
【发布时间】:2015-07-18 21:41:01
【问题描述】:

我要调整位图大小并插入到 imageView 中。

但是我遇到了回收错误...

错误信息:

05-08 13:32:48.948: E/AndroidRuntime(4792): Process: com.test.myapp, PID: 4792 05-08 13:32:48.948: E/AndroidRuntime(4792): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2f1aa6ad 05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225) 05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:600) 05-08 13:32:48.948: E/AndroidRuntime(4792): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:544) 05-08 13:32:48.948: E/AndroidRuntime(4792): at android.widget.ImageView.onDraw(ImageView.java:1187) 05-08 13:32:48.948: E/AndroidRuntime(4792): at android.view.View.draw(View.java:16209)

您能给我建议如何解决我的情况吗..

我的代码:

Bitmap photo = BitmapFactory.decodeFile(full_path);
SaveBitmapToFileCache(photo, Environment.getExternalStorageDirectory().getPath() + "/test/"+filename);

imgview.setImageBitmap(photo);

private Bitmap getBitmapSize(Bitmap photo){
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Bitmap bmp = photo;
    Bitmap resizeBmp = null;
    if(bmp.getWidth() > 2000 || bmp.getHeight() > 2000)
        resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/10 , bmp.getHeight()/10, false);
    else if(bmp.getWidth() > 1000 || bmp.getHeight() > 1000)
        resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth()/5 , bmp.getHeight()/5, false);
    else
        resizeBmp = resizingBitmap(mContext, bmp, bmp.getWidth() , bmp.getHeight(), false);

    resizeBmp.compress(CompressFormat.PNG, 90, os);
    bmp.recycle();

    return resizeBmp;
}

private void SaveBitmapToFileCache(Bitmap bitmap, String strFilePath) {
    File fileCacheItem = new File(strFilePath);
    OutputStream out = null;

    try{
        fileCacheItem.createNewFile();
        out = new FileOutputStream(fileCacheItem);

        bitmap.compress(CompressFormat.JPEG, 100, out);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally{
        try{
            out.close();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}

为什么会遇到回收错误?

【问题讨论】:

    标签: android canvas bitmap imageview recycle


    【解决方案1】:

    要修复 OutOfMemory 错误,您应该执行以下操作:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    
    Bitmap preview_bitmap = BitmapFactory.decodeStream(is, null, options);
    

    这个 inSampleSize 选项减少了内存消耗。

    这是一个完整的方法。首先它读取图像大小而不解码内容本身。然后找到最佳的 inSampleSize 值,应该是 2 的幂,最后解码图像。

    // Decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File 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) {}
    return null;
    

    }

    【讨论】:

    • 谢谢 :) 我会尝试考虑位图选项(例如:inSampleSize)。我不知道内存泄漏...
    【解决方案2】:

    使用这几行代码..

    if(resizeBmp !=null)
    {
      resizeBmp .recycle();
      bmp.recycle();
      bmp=null;
      resizeBmp =null;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多