0

我在相机中工作。我想从我的画廊中选择图像并裁剪选定的照片,然后在我的 imageview 中显示它。我写了一些代码,但我在裁剪时遇到了问题。这是我的错误

 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();
        }
    }
}

这是我的代码我在裁剪图像中遇到问题,因为当我删除裁剪时,我的应用程序运行完美,我做错了什么?如果有人知道解决方案,请帮助我

4

1 回答 1

2
  1. 选择 Action_Pick 意图

                            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 从 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. 应用裁剪操作

    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);
     }
    
于 2015-11-16T11:25:20.810 回答