-2

我目前正在为我的应用程序处理用户个人资料。直到最近,我一直在测试三星 Galaxy Note 4,它并没有给我下面的代码带来任何问题。然而,在后来的测试中,我拿到了 Nexus 5x,当我使用下面的代码尝试选择用户个人资料图像时,我收到错误“不幸的是照片已停止”。我没有任何错误日志,因为我无法调试照片。

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

任何帮助将不胜感激。

4

1 回答 1

1

Android 没有CROP Intent. 您的代码将在数亿台设备上失败。

有许多适用于 Android 的图像裁剪库。请使用一个。

于 2016-06-30T14:09:07.987 回答