-2

I am developing an Android application that allows a user to crop an image which is in my Drawable folder. This is the code I am using but it is showing errors. How can I fix this?

   @Override
public void onClick(View v) {

    Uri imgUri=Uri.parse("android.resource://com.example.cropapp/"+R.drawable.apples);
     Intent intent = new Intent("com.android.camera.action.CROP");  
            intent.setDataAndType(imgUri, "image/*");  
            intent.putExtra("crop", "true");  
            intent.putExtra("aspectX", 1);  
            intent.putExtra("aspectY", 1);  
            intent.putExtra("outputX", 80);  
            intent.putExtra("outputY", 80);  
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1);
    }
Coder1
  • 55
  • 1
  • 1
  • 4

2 Answers2

4

How can I fix this?

First, recognize that Android does not have a CROP Intent. There are many image cropping libraries for Android. Use one.

Second, recognize that few, if any, apps advertise <intent-filter> structures that support the ill-used android.resource scheme. This will not be a problem when you switch to an image-cropping library, as then everything will be in your own app.

However, do bear in mind that the vast majority of image-cropping scenarios involve images that are files, not resources, let alone drawable resources. It is entirely possible that you are the first person in human history to want to allow users to crop drawable resources, and so you are likely to have to blaze your own trail a bit.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I can't completely agree with you, check this accepted answer http://stackoverflow.com/a/15239086/2506025 – Prokash Sarkar Aug 08 '15 at 18:45
  • @ProkashSarkar: Feel free to provide a link to where the `CROP` `Intent` is documented in the Android SDK. Feel free to provide a link to where Google requires device manufacturers to offer a `CROP` `Intent` in order to have the Play Store and other Google proprietary apps on their devices. – CommonsWare Aug 08 '15 at 18:50
  • I got confused by many post where people claims that its working fine with this approach. After some search I got the point, it's actually supported but not officially. Thanks for figuring out the issue, I've updated my answer. – Prokash Sarkar Aug 08 '15 at 19:12
  • @ProkashSarkar: "it's actually supported" -- no, it is not. Android itself has no `CROP` `Intent`. *Camera apps* -- or, theoretically, other apps -- *may* offer a `CROP` `Intent`. Few do. Given a choice between using a library or relying upon a potentially non-existent activity, I would hope that most developers would use the library. – CommonsWare Aug 08 '15 at 19:26
0

The following code snippet is for custom roms like AOSP Camera app, and not Officially available or supported by most of the devices

Possible Solution?

Try an unofficial library like this one,

https://github.com/lvillani/android-cropimage

Code example:

@Override
    public void onClick(View view) {
        if (view.equals(button)) {
            startActivityForResult(MediaStoreUtils.getPickImageIntent(this), REQUEST_PICTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        File croppedImageFile = new File(getFilesDir(), "test.jpg");

        if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) {
            // When the user is done picking a picture, let's start the CropImage Activity,
            // setting the output image file and size to 200x200 pixels square.
            Uri croppedImage = Uri.fromFile(croppedImageFile);

            CropImageIntentBuilder cropImage = new CropImageIntentBuilder(200, 200, croppedImage);
            cropImage.setOutlineColor(0xFF03A9F4);
            cropImage.setSourceImage(data.getData());

            startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
        } else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) {
            // When we are done cropping, display it in the ImageView.
            imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath()));
        }
    }
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • "The Android Contact manager EditContactActivity uses Intent("com.android.camera.action.CROP") which can be used to crop the Image" -- no, it does not. First, there is no such activity anymore, as you can tell by looking at [the manifest](https://android.googlesource.com/platform/packages/apps/Contacts/+/master/AndroidManifest.xml). Second, [the contact editor activity](https://android.googlesource.com/platform/packages/apps/Contacts/+/master/src/com/android/contacts/activities/ContactEditorActivity.java) does not use a `CROP` `Intent`. – CommonsWare Aug 08 '15 at 19:23
  • Even if you go back a ways in the version history, to when the AOSP Contacts app had an `EditContactActivity`, it still did not use a `CROP` `Intent`, at least not in all versions. See [this version](https://android.googlesource.com/platform/packages/apps/Contacts/+/34aeeb0f956357fedbe16fbb0fd1172c9bdfaa1a/src/com/android/contacts/ui/EditContactActivity.java), for example. It's certainly possible that, once upon a time, there was an `EditContactActivity` that used such an `Intent`, but not anymore. – CommonsWare Aug 08 '15 at 19:25
  • Thanks for clearing out the issue. In fact its an honor to get the mistakes corrected from you. Hope that anyone goes through this post will not try it again, since it's wrongly described in many post/tutorials. – Prokash Sarkar Aug 08 '15 at 19:36