-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
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 |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not dummy. Include a better explanation, something like:
|
||
*/ | ||
public class ImageCropperFileProvider extends FileProvider | ||
{ | ||
} |
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="." /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
</paths> |
There was a problem hiding this comment.
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
.