Crop does not work for gallery images

前端 未结 3 1861
闹比i
闹比i 2020-12-07 05:00

I am using the following code to crop images from camera and gallery :

private void doCrop() {
    final ArrayList cropOptions = new ArrayL         


        
3条回答
  •  孤街浪徒
    2020-12-07 05:21

    Hi Can u Try this,

    @Override
        public void onActivityResult(final int requestCode, int resultCode,
                Intent data) {
            try {
                switch (requestCode) {
                case GALLERY_PIC_REQUEST:
                    if (resultCode == RESULT_OK) {
                        try {
                            picUri = data.getData();
                            System.out.println("Image Path::>  "+picUri.getPath());
                            performCrop();
                        } catch (Exception e) {
                            ShowDialog_Ok("Error", "Couldn't load photo");
                        }
                    }
                    break;
                case CAMERA_PIC_REQUEST:
                    if (resultCode == RESULT_OK) {
                        try {
                            picUri = data.getData();
                            System.out.println("Image Path::>  "+picUri.getPath());
                            performCrop();
                        } catch (Exception e) {
                            ShowDialog_Ok("Error", "Couldn't load photo");
                        }
                    }
                    break;
                case CROP_PIC_REQUEST:
                    Bitmap bitmap;
                    String path = "";
                    if (resultCode == RESULT_OK) {
                        String oldPath = fashionlyPreferences.getImagePath();
                        try {
                            picUri = data.getData();
                            path = Helper.GetPathFromURI(context, picUri);
                            bitmap  = BitmapFactory.decodeFile(path);
                            Img_View.setImageBitmap(bitmap);
                        } catch (Exception e) {
                            ShowDialog_Ok("Error", "Couldn't load photo");
                        }
                    }
    
                    break;
                }
            } catch (Exception e) {
            }
        }
    

    //Function for Crop

    private void performCrop() {
                    // take care of exceptions
                    try {
                        if (picUri!=null) {
                             Intent cropIntent = new Intent("com.android.camera.action.CROP");
                             cropIntent.setDataAndType(picUri, "image/*");
                             cropIntent.putExtra("crop", "true");
                             cropIntent.putExtra("scale", true);
                             cropIntent.putExtra("aspectX", 0);
                             cropIntent.putExtra("aspectY", 0);
                            //cropIntent.putExtra("outputX", 400); 
                            // cropIntent.putExtra("outputY", 400);
                             cropIntent.putExtra("return-data", false);
                             startActivityForResult(cropIntent, CROP_PIC_REQUEST);
                        }
                    }
                    // respond to users whose devices do not support the crop action
                    catch (ActivityNotFoundException anfe) {
                        Toast.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT).show();
                    }
             }
    

提交回复
热议问题