Search code examples
androidbitmapandroid-bitmapbitmapimagebitmapdata

How to relase bitmap from a return method correctly?


Will this method will recycle the bitmap in imageUtils.mediaImageToBitmap(image, activity);?.

because I can't put recycle after the return methodBitmap;

Bitmap bitmap = imageUtils.mediaImageToBitmap(image, activity);
//some process
...
//some process
bitmap.recycle();

Update

After I'm done I put bitmap.recycle() but in imageUtils.mediaImageToBitmap(image, activity); there's a bitmap inside that function, does it require to be recycle as well?

or

bitmap.recycle() will automatically recycle the bitmap inside imageUtils.mediaImageToBitmap(image, activity);?

    public Bitmap mediaImageToBitmap(Image image, Context context) {
        Bitmap bitmap;
        //Do I need to do bitmap recycle in this method's Java Class?
        //or 
        //The other class that call this method 
        //Bitmap bitmap = imageUtils.mediaImageToBitmap(image, activity);
        //Then bitmap.recycle is enough?
        return bitmap;
    }

Solution

  • Your imageUtils.mediaImageToBitmap(image, activity); should only be returning reference to bitmap object which you assign to your local variable. In other words there is only one bitmap object and calling bitmap.recycle() once will automatically recycle that bitmap object.