Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom FileProvider to fix FileUriExposedException when using camera as image source #688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cropper/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<manifest
package="com.theartofdev.edmodo.cropper">

<manifest package="com.theartofdev.edmodo.cropper"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<provider
android:name=".ImageCropperFileProvider"
android:authorities="${applicationId}.fileprovider"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.fileprovider is easily the default suffix for anyone starting using file providers.

Use a library-specific suffix, like ${applicationId}.com.theartofdev.edmodo.cropper.fileprovider.

android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

/**
Expand Down Expand Up @@ -343,7 +344,8 @@ public static Uri getCaptureImageOutputUri(@NonNull Context context) {
Uri outputFileUri = null;
File getImage = context.getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
File pickImageFile = new File(getImage.getPath(), "pickImageResult.jpeg");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the path to reflect changes suggested by other comments. Maybe abstract the file factory in a static method on the custom file provider class. And some unit tests never hurt.

(You're contributing to a solution used by thousands, it's not just a quick workaround in your app anymore. Make it so it doesn't break again in a month.)

outputFileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", pickImageFile);
}
return outputFileUri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.theartofdev.edmodo.cropper;

import androidx.core.content.FileProvider;

/**
* Dummy file provider

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not dummy. Include a better explanation, something like:

/**
 * Library-specific subclass of [FileProvider] so that the original class can still be declared by the consuming app.
 * See https://commonsware.com/blog/2017/06/27/fileprovider-libraries.html for details.
 */

*/
public class ImageCropperFileProvider extends FileProvider
{
}
4 changes: 4 additions & 0 deletions cropper/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path name="external_cached_cropper_files" path="." />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You're in root of app-specific cache directory. Work with library-specific directory in the cache directory instead.

  • name should reflect what's inside the directory. This Uri is only shared with camera apps, so name it accordingly.

  • cropper_files is implicit, it's the library's content provider. external is an implementation detail, Uri is meant to abstract the real storage.

</paths>