0

When I open gallery and select a image app get crash with the exception "java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes"

The code is as follow

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY);

and in On activity result method

openDialog.dismiss();
    try {
    if (data == null || data.getData() == null) {
        Toast.makeText(getContext(), "Error getting image.", Toast.LENGTH_SHORT).show();
        return;
    }
    mUri = data.getData();
    createFile(mUri, null);
} catch (Exception e) {
    Log.e(TAG, "GALLERY EXCEPTION " + e.toString());
} catch (OutOfMemoryError E) {
    Log.e(TAG, "GALLERY MEMORY EXCEPTION " + E.toString());
}

I am not using onSavedInstancestate(). and I've reffered

What to do on TransactionTooLargeException

and

http://nemanjakovacevic.net/blog/english/2015/03/24/yet-another-post-on-serializable-vs-parcelable/

Komal12
  • 3,340
  • 4
  • 16
  • 25

2 Answers2

0

Do not exchange huge data (>1MB) between services and application. We can't send image/data through intent size > 1MB. TransactionTooLargeException occurred when you tried to send large bitmap image/image/pojo from one activity to another via intent.

Solution : Use global variable for this.

You are getting exception because of this:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY);

If image size will < 1MB sure it will work, but I'm sure image size will be >1MB.

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
0

You need to resize your image size before set into imageview (if you having very large image then you need to resize your image in thread).

So you need to call createFile(this,mUri) and it will return you bitmap. I already put height and width hardcoded for now so you can change yourself.

/**
 * Loads a bitmap and avoids using too much memory loading big images (e.g.: 2560*1920)
 */
private static Bitmap createFile(Context context, Uri theUri) {
    Bitmap outputBitmap = null;
    AssetFileDescriptor fileDescriptor;

    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");

        BitmapFactory.Options options = new BitmapFactory.Options();
        outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
        options.inJustDecodeBounds = true;

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;

        float maxHeight = 740.0f;
        float maxWidth = 1280.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }
        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inTempStorage = new byte[16 * 1024];
        outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
        if (outputBitmap != null) {
            Log.d(TAG, "Loaded image with sample size " + options.inSampleSize + "\t\t"
                    + "Bitmap width: " + outputBitmap.getWidth()
                    + "\theight: " + outputBitmap.getHeight());
        }
        fileDescriptor.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputBitmap;
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    final float totalPixels = width * height;
    final float totalReqPixelsCap = reqWidth * reqHeight * 2;
    while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
        inSampleSize++;
    }

    return inSampleSize;
}
duggu
  • 37,851
  • 12
  • 116
  • 113