Why do I get a TransactionTooLargeException?

178 Views Asked by At

I am working on an Android Project but I am stuck when I need to take a picture from gallery. Indeed, when I take the first picture, everything is going well. But, if I take a second picture, the exception "TransactionTooLargeException" is thrown and my Application crashed.

The code used to start the activity :

public void addImage(View view) {
        if(isPermissionEnable) {
            try {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), ADD_IMAGE);
            } catch (Exception ex) {
                GraphicUtils.displayPopup(this, getString(R.string.warning), 
                getString(R.string.image_too_large));
            }
        }else
        {
            askPermissions();
        }

    }

The code to get the result :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case ADD_IMAGE:
                    if(getContentResolver().getType(data.getData()).contains("jpeg") || getContentResolver().getType(data.getData()).contains("jpg") || getContentResolver().getType(data.getData()).contains("png") || getContentResolver().getType(data.getData()).contains("bmp")) {

                        Uri uri = data.getData();

                        try {
                            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                            int minimumSize = (bitmap.getWidth() < bitmap.getHeight()) ? bitmap.getWidth() : bitmap.getHeight();
                            finalBitmap = ThumbnailUtils.extractThumbnail(bitmap, minimumSize, minimumSize);
                            imageView.setImageBitmap(finalBitmap);
                            button_right.setVisibility(View.VISIBLE);
                            button_left.setVisibility(View.VISIBLE);

                        } catch (Exception e) {
                            e.printStackTrace();
                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
                            builder.setMessage(uri.toString());
                            builder.setTitle("Erreur");
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                    else{
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setMessage(getContentResolver().getType(data.getData()));
                        builder.setTitle("Erreur");
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                    break;
                default :
                    break;
            }
        }
    }

Do you have any idea to solve my problem please ?

1

There are 1 best solutions below

3
On

From the dcoumentation:

The key to avoiding TransactionTooLargeException is to keep all transactions relatively small. Try to minimize the amount of memory needed to create a Parcel for the arguments and the return value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps. If possible, try to break up big requests into smaller pieces.

I am guessing you are performing several transactions or quite possibly one that is quite large.

Try to analyze and assess in your application where you could limit and curb the amount of data your are requesting.

Also, look at this SO question for more information.