Replace gallery image with gallery image in android show too large exception

心已入冬 提交于 2021-02-17 06:01:16

问题


java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 51924760 bytes when one image is select from gallery is work but when i am try to update same image with new image from gallery in android it show this error and application crash

Solution for this problem is given below

// when you pass bitmap through listview adapter to get this in Fragment
 imageBitMap = getArguments().getParcelable("bitmapImage");
            profileImageView.setImageBitmap(imageBitMap);

//Add this position of code in onCreate
 pictureSelectionAlertBox = new AlertDialog.Builder(getContext());
        profileImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                        ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                        ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    pictureSelectionAlertBox.setTitle("Select Picture Option");
                    pictureSelectionAlertBox.setItems(Options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == 0) {
                                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                                startActivityForResult(takePicture, 0);

                            } else if (which == 1) {
                                // imageBitMap = null;
                                Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(pickPhoto, 1);

                            } else {
                                Toast.makeText(getContext(), "Option not Selected ", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    pictureSelectionAlertBox.show();

                } else {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }

            }
        });




//this Code is write outside oncreate
  @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                imageBitMap = (Bitmap) extras.get("data");
                profileImageView.setImageBitmap(imageBitMap);
            }
        }
        if (resultCode == RESULT_OK) {

            selectedImageUri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            if (selectedImageUri != null) {
                Cursor cursor = getContext().getContentResolver().query(selectedImageUri,
                        filePathColumn, null, null, null);
                if (cursor != null) {
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    imageBitMap = getImageFromPath(picturePath);
                    if (imageBitMap != null) {
                        profileImageView.setImageBitmap(imageBitMap);

                    }
                    cursor.close();
                }
            }
        }
    }
//this fuction convert string path to bitmap
public Bitmap getImageFromPath(String path) {
        File image = new File(path);
        if (image.exists()) {
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
            bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
            return bitmap;
        }
        return null;
    }

//this code below is for check permission are granted or not
 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==1){
            boolean allow = false;
            for (int granted : grantResults){
               if (granted == PackageManager.PERMISSION_GRANTED){
                   allow = true;
               }
            }

            if (allow){
                Toast.makeText(getActivity(), "Permission Granted", Toast.LENGTH_LONG).show();
            }
        }
    }

来源:https://stackoverflow.com/questions/64967738/replace-gallery-image-with-gallery-image-in-android-show-too-large-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!