0

Every time I crop an image using a camera I got an error Unable to load image. But in the case of the gallery, it worked fine.

Uri uriPath = StoreAndFetchImageFromFile.getInstance(ParentDetails.this).getImageUri(partFilename);
                selectedimagepath =  getPath(uriPath);

                Bitmap myBitmap = BitmapFactory.decodeFile(selectedimagepath);
                parentimage.setImageBitmap(myBitmap);
                performCropCamera(uriPath);

And method for imagecrop is:

 private void performCropCamera(Uri picUri) {
    // take care of exceptions
    try {
        // call the standard crop action intent (the user device may not
        // support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        int asp = (int) (DeviceDimensions.getScreenWidth() - 80)/187;
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectX", asp);
        cropIntent.putExtra("aspectY", 3);
        // indicate output X and Y
        cropIntent.putExtra("outputX", DeviceDimensions.getScreenWidth() - 80);
        cropIntent.putExtra("outputY", 187*3);
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        Toast toast = Toast
                .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
        toast.show();
    }
}

And the OnActivity result for image crop is:

 if (requestCode == PIC_CROP) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            parentimage.setImageBitmap(thePic);
        }
BalaajiChander
  • 139
  • 3
  • 21
Vinay
  • 11
  • 6
  • 2
    please note that the crop feature is not mandatory for Android, and some devices might not have `com.android.camera.action.CROP`. So it's bad idea to use external crop feature. I'd better find a library for crop, and use it. – Vladyslav Matviienko Jun 12 '17 at 07:12
  • Ok. Thnx but why is this happening, in the case camera..? – Vinay Jun 12 '17 at 07:19
  • Possible duplicate of [No Activity found to handle Intent com.android.camera.action.CROP](https://stackoverflow.com/questions/41890891/no-activity-found-to-handle-intent-com-android-camera-action-crop) – W4R10CK Jun 12 '17 at 07:21
  • that's a question to your device's firmware provider I think. – Vladyslav Matviienko Jun 12 '17 at 07:27
  • Not a duplicate. CROP is screwy but the question is sound. Why does the image coming from the camera work, but the image coming from the crop does not. And odds are good it's the clusterfrack of where various services put the data. – Tatarize Jun 12 '17 at 07:37
  • @Tatarize rather it gives error 'unable to find picture' before moving to crop activity. – Vinay Jun 12 '17 at 07:43
  • Then does the crop activity get the image? Because if it did, likely it owned the picture after it grabbed it. – Tatarize Jun 12 '17 at 07:49
  • Or some permission issue where your app gets the permissions to the file in the other app, but it doesn't have the permissions to just give it to the crop app willynilly. If you want the crop app to read it from the camera that might be an issue with the permission between the three different apps here. – Tatarize Jun 12 '17 at 07:51

1 Answers1

0

It likely comes back in a different format after the command. Files in intents can hide in a few different places, they can be accessed a few different ways to get the stream to boot. There's also a few items with permissions. The camera intent might give you the data one way, and the crop intent might returned the cropped data in a different way. So you can't expect the two to be the same. You have to cover all the bases.

And keep in mind this is just the CROP function for your device. Other devices don't have a crop function and they also might work different still. I wouldn't trust the lot of them. It's literally drawing box and painting a bitmap in another bitmap. I use a 3rd party library and include it in your app. Then you can be sure it worked.

While it sounds good. You cannot be sure that that functionality exists. Much less that it will work in a consistent manner.

If I recall the file can be in the extras in the file stream. It can be a content:// or a file:// object. And the permissions might get trixy. The galleries tend to return them as a content:// file without the named suffix whereas the camera might give file suffix that can be read.

I've seen this stuff a few times where you need to lookup the name for the stuff returned from the gallery to know the filetype whereas other times the URI it includes properly gives you the correct suffix.

It's these and other reasons that basically make a crop intent functionally worthless. If I had to guess the file is stored in a different way than you expect when it returns from crop. I use something like:

public Uri getUriFromIntent(Intent intent) {
    Uri uri = intent.getData();
    if (uri != null) return uri;

    Bundle bundle = intent.getExtras();
    if (bundle == null) return null;

    Object object = bundle.get(Intent.EXTRA_STREAM);
    if (object instanceof Uri) {
        return (Uri) object;
    }
    return null;
}

In hopes of finding where it might be, because different things put the file in a bunch of different places and it's really screwy trying to find them if you're not sure the exact service that gave the URI to you.

Tatarize
  • 10,238
  • 4
  • 58
  • 64