"Unfortunately Photos has stopped" 尝试拍照和使用裁剪功能时

"Unfortunately Photos has stopped" when trying to take pictures and use crop function

我目前正在为我的应用处理用户配置文件。直到最近,我一直在 Samsung Galaxy Note 4 上进行测试,它没有给我以下代码带来任何问题。然而,在后来的测试中,我得到了 Nexus 5x,当我使用下面的代码尝试 select 用户个人资料图像时,我收到错误 "Unfortunately Photos has stopped"。我没有任何错误日志,因为我无法调试照片。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                cropImage(selectedImage);
            }

            break;
        case 1:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                cropImage(selectedImage);
            }
            break;
        case 2:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                profileImage.setImageURI(selectedImage);
                try {
                    userProfileImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
                    AlmightyFunctions.ImageService.saveProfileImage(user,userProfileImage);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

public void showImageSelectDialog() {
    CharSequence options[] = new CharSequence[] {
            getString(R.string.select_camera),
            getString(R.string.select_gallery)
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.select_image_dialog));
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            MarshMallowPermission mmp = new MarshMallowPermission(UserProfileActivity.this);
            switch(which) {
                case 0:
                    if (!mmp.checkPermissionForCamera()) {
                        mmp.requestPermissionForCamera();
                    }
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, 0);
                    break;
                case 1:
                    if (!mmp.checkPermissionForExternalStorage()) {
                        mmp.requestPermissionForExternalStorage();
                    }
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, 1);
                    break;
            }
        }
    });
    builder.show();
}

public void cropImage(Uri photoUri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(photoUri, "image/*"); // this will open all images in the Gallery
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1); // this defines the aspect ration
    intent.putExtra("aspectY", 1);
    intent.putExtra("return-data", true); // true to return a Bitmap, false to directly save the cropped iamge
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); //save output image in uri
    startActivityForResult(intent,2);
}

如有任何帮助,我们将不胜感激。

Android does not have a CROP Intent。您的代码将在数亿台设备上失败。

有很多image cropping libraries available for Android。请使用一个。