0

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 ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hayato175
  • 21
  • 5

1 Answers1

0

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.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
  • The problem is that I can't manage data sended because this action is automatically handled by "Intent.ACTION_PICK" – Hayato175 Mar 21 '20 at 09:07
  • @Hayato175 - but you are requesting data from some backend. Consider sending that data in smaller packets. In the link I added, reason #3 is the one you are dealing with. If my answer helped, please mark it as such. – tomerpacific Mar 21 '20 at 09:56
  • @Hayato175 - if my answer helped, please mark it as such. – tomerpacific Mar 25 '20 at 07:49