Search code examples
javaandroidgoogle-photos

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


I'm currently working on the user profile for my app. Up until recently, I've been testing on a Samsung Galaxy Note 4, which hasn't been giving me any problems with the code below. In later testing, however, I got my hands on a Nexus 5x, and when I use the below code to try and select a user profile image, I get the error "Unfortunately Photos has stopped". I don't have any error logs, since I am unable to debug Photos.

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

Any help would be greatly appreciated.


Solution

  • Android does not have a CROP Intent. Your code will fail on hundreds of millions of devices.

    There are many image cropping libraries available for Android. Please use one.