New Image File From URI Does Not Exist

598 views Asked by At

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>
0

There are 0 answers