0

I am compressing Bitmap in asynctask and sending it to another activity through Bundle and I am getting this crash. I am calling Bitmap.recycle() in my code. Sometime it works properly, here is my Logcat output.

Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getConfig() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called getConfig() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.863 21779-22017/ W/Bitmap: Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
2020-01-14 16:25:56.864 21779-22017/ A/Bitmap: Error, cannot access an invalid/free'd bitmap here!
2020-01-14 16:25:56.864 21779-22017/ A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 22017 (AsyncTask #5), pid 21779 

Here is the code to Compress Image

private static void compressImage(final Bitmap bitmap, final Callback<Bitmap> gbCallback) {

    new AsyncTask<Void, Void, Bitmap>() {
        protected void onPreExecute() {
        }

        @Override
        protected Bitmap doInBackground(Void... params) {

            Bitmap image = getScaledImageCopy(bitmap);
            if (image != null) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);

                byte[] byteArray = byteArrayOutputStream.toByteArray();
                image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                return image;
            } else {
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            gbCallback.call(bitmap);
        }
    }.execute(null, null, null);

 private static Bitmap getScaledImageCopyForUGC(Bitmap image) {
    try {
        int height = image.getHeight();
        int width = image.getWidth();
        return Bitmap.createScaledBitmap(image, 400, (400 * height) / width, true);
    } catch (Exception e) {
        if (image != null) {
            return Bitmap.createScaledBitmap(image, 300, 300, true);
        } else {
            return null;
        }
    }
}
user3606902
  • 829
  • 1
  • 10
  • 25

1 Answers1

0

Its seems like the bitmap is already recycled. You cant use a recycled bitmap. You need to make sure that the bitmap is not recycled which is passed in compressImage. For now you can check if the bitmap is recycled or not to avoid error.

if(!bitmap.isRecycled()){
    //bitmap is not recycled
}else {
    //bitmap is recycled, use a default placeholder
}
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49