-2

I'm goning to resize bitmap and insert into imageView.

However I encountered recycle error...

error message :

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)

Could you give an advice how to resolve my situation..

My Code :

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();
        }
    }
}

Why do I meet recycle error?

BBdev
  • 4,898
  • 2
  • 31
  • 45
Richard
  • 351
  • 4
  • 17

1 Answers1

0

Use these lines of code..

if(resizeBmp !=null)
{
  resizeBmp .recycle();
  bmp.recycle();
  bmp=null;
  resizeBmp =null;
}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103