android crop image from gallery nullpointexception

2019-09-19 04:08发布

问题:

I'm working in Camera.i want to choose image from my gallery and crop selected photo and then show it in my imageview.i wrote some code but i have problem in cropping. this is a my error

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference




  private void CropPictureFromGallery()
{



    Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    pickImageIntent.setType("image/*");
    pickImageIntent.putExtra("crop", "true");
    pickImageIntent.putExtra("outputX", 200);
    pickImageIntent.putExtra("outputY", 200);
    pickImageIntent.putExtra("aspectX", 1);
    pickImageIntent.putExtra("aspectY", 1);
    pickImageIntent.putExtra("scale", true);
    pickImageIntent.putExtra("outputFormat",
    startActivityForResult(pickImageIntent, PICK_FROM_GALLERY);

}




  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK){
        Uri targetUri = data.getData();
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
            mAvatar.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

this is a my code I have problem in Cropping image because when i removed crop my app working perfect what am i doing wrong? if anyone knows solution please help me

回答1:

  1. Select Action_Pick intent

                            Intent intent;
                            intent = new Intent(
                                    Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            Intent chooser = Intent.createChooser(intent,
                                    "Choose a Picture");
                            startActivityForResult(chooser,
                                    RequestCode.REQ_GALLERY);
    
  2. OnActivityResult get the image from URI

           case RequestCode.REQ_GALLERY:
            if (resultCode == Activity.RESULT_OK) {
                Uri PhotoURI = data.getData();
                Bitmap bitmapImage = null;
                try {
                    bitmapImage = decodeBitmap(PhotoURI);
                    BitmapFactory.decodeStream(getCurrActivity().getContentResolver().openInputStream(PhotoURI));
    
                    doCrop(getImageUri(getCurrActivity(), bitmapImage));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
    
    public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
     }
    
  3. Apply crop operation

    private void doCrop(Uri mCurrentPhotoPath) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(mCurrentPhotoPath, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    cropIntent.putExtra("outputX", 320);
    cropIntent.putExtra("outputY", 320);
    
    File cameraFolder;
    
    cameraFolder = new File(AppConstants.BASE_FOLDER);
    
    if (!cameraFolder.exists()) {
        cameraFolder.mkdirs();
    }
    
    mSourceFileName = "/IMG_" + System.currentTimeMillis() + ".jpg";
    
    File photo = new File(cameraFolder, mSourceFileName);
    try {
        photo.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    Uri mCropImageUri = Uri.fromFile(photo);
    
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImageUri);
    
    startActivityForResult(cropIntent, RequestCode.REQ_CROP_IMG);
     }