1

So I'm using the Android Crop library (https://github.com/jdamcd/android-crop) to allow a user to take a picture and then crop it.

I need to create two files: one that contains the original image from the camera and one that contains the cropped image. I do this by defining two Files and two URI's that I then pass into the Android Crop library:

// Camera file.
Calendar calendar = Calendar.getInstance();
cameraCaptureFile = new File(getExternalCacheDir(), "camera-capture-" + calendar.getTime().getTime() + ".tmp");
cameraCaptureUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", cameraCaptureFile);
grantUriPermission(getPackageName(), cameraCaptureUri, FLAG_GRANT_WRITE_URI_PERMISSION & FLAG_GRANT_READ_URI_PERMISSION);

// Cropped file.
croppedFile = new File(getExternalCacheDir(), "cropped.png");
croppedFileUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", croppedFile);
grantUriPermission(getPackageName(), croppedFileUri, FLAG_GRANT_WRITE_URI_PERMISSION & FLAG_GRANT_READ_URI_PERMISSION);

// Start cropping library.
Crop.of(cameraCaptureUri, croppedFileUri)

// Cropping complete lets get our cropped file.
File croppedPng = new File(croppedFileUri.getPath());

The problem is that croppedPng.exists() always returns false; however, when I go into the device storage browser and navigation to Android/data/com.mycompany/cache both the original and cropped files are there as expected.

I'm not quite sure what to make of this since my app was able to create the two files but then it cannot read them? Does not make sense.

Just for completion here are my other configurations related to this problem:

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="external_files" path="." />
</paths>

AndroidManifest.xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mycompany.provider"
        android:exported="false"
        android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/provider_paths"/>
 </provider>
Przemek Lach
  • 1,348
  • 2
  • 19
  • 42
  • you dont need `croppedFileUri.getPath()` - you need `croppedFileUri`'s input stream - see `ContentResolver` API on how to get it – pskink Sep 17 '17 at 06:47
  • what do you want to do with the cropped image btw? – pskink Sep 17 '17 at 07:04
  • I convert it to a byte[] and upload to a server. Your first answer worked btw. Can you make a formal reply so that I can mark it as answer. Thank you very much! – Przemek Lach Sep 17 '17 at 07:13
  • 1
    you're welcome, glad you managed to glue the pieces together – pskink Sep 17 '17 at 07:23

0 Answers0