1

I am trying to make an app to select an image from the gallery or google photos and then crop it to make it my app background. The problem I am facing is that When I try to crop the picture using google photos then it gets saved but the app background doesn't change and I don't recive any crashes or any errors but when I crop it using the gallery app everything seems to work perfectly.

Here is my code to select photos:

Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(i, SELECT_PICTURE);

case SELECT_PICTURE: {
            if (resultCode == RESULT_OK && null != data) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        Uri selectedImageUri = data.getData();
                        InputStream imageStream;
                        Bitmap selectedImage;
                        try {
                            cropCapturedImage(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
                        } catch (ActivityNotFoundException aNFE) {
                            //display an error message if user device doesn't support
                            showToast(getString(R.string.error_crop_not_supported));
                            try {
                                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                                Cursor cursor = getContentResolver().query(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri), filePathColumn, null, null, null);
                                cursor.moveToFirst();
                                imageStream = getContentResolver().openInputStream(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
                                selectedImage = BitmapFactory.decodeStream(imageStream);

                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                byte[] b = baos.toByteArray();
                                String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                                //SharePreference to store image
                                PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
                                cursor.close();
                                //set gallery image
                                setChatBackground();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }

Here is my cropIntent code:

public void cropCapturedImage(Uri picUri) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(picUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 9);
    cropIntent.putExtra("aspectY", 14);
    cropIntent.putExtra("outputX", 256);
    cropIntent.putExtra("outputY", 256);
    cropIntent.putExtra("return-data", true);
    startActivityForResult(cropIntent, CROP_PICTURE);
}

ase CROP_PICTURE: {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
            if(cursor.moveToFirst()){
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String yourRealPath = cursor.getString(columnIndex);
            } else {
                //boooo, cursor doesn't have rows ...
            }
            cursor.close();
            String v= data.toString();

            if (resultCode == RESULT_OK && null != data) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Bundle extras = data.getExtras();
                            Bitmap thePic = extras.getParcelable("data");
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            thePic.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            byte[] b = baos.toByteArray();
                            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                            //SharePreference to store image
                            PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
                            //set gallery image
                            setChatBackground();
                        } catch (NullPointerException e) {
                            Log.d(TAG, e.getLocalizedMessage());
                        }
                    }
                });
            }
  • Possible duplicate of [No Activity found to handle Intent com.android.camera.action.CROP](http://stackoverflow.com/questions/41890891/no-activity-found-to-handle-intent-com-android-camera-action-crop) – W4R10CK Feb 02 '17 at 08:59

2 Answers2

1

No, Android Does Not Have a Crop Intent

Calling method startActivity() on an Intent with an action of com.android.camera.action.CROP. They are doing this to crop an image.

This is a really bad idea.

Source here - CommonsBlog

enter image description here

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • I tried with the ivilani crop image library and replaced the cropCapturedImage() function codes with the one provided in its example app and passed the intent to set background image. With this also the image is getting cropped and saved but still there is no change in my application background. It is similar to the issue I am facing with the google photos using Crop Intent. Thanks. – anant kumar Feb 02 '17 at 09:57
0

Try to log your selectedImageUri first.

I suspect that the google photos returns a url to online file, while the gallery app return a local file path.

Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31
  • Malmygin That was the problem at first and I was unable to select photos from google photos I solved that by writing image to temp file and then getting image uri with authority. Now the problem is with the crop intent. The gallery app and google photos are providing different crop intent data. How do I overcome that? Thanks. – anant kumar Feb 02 '17 at 09:52
  • Well, DO NOT use crop intent. Use Intent only for photo selection and after apply crop with one of a library suggested by @w4r10ck. – Anton Malmygin Feb 02 '17 at 10:36