-
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?
Conversation
Add custom FileProvider
Hi @ArthurHub, can you merge this PR pls. I also get crashs on Android 10 devices an this PR will fix them. |
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.
Hi, I left some comments on your PR. You're going in the right direction but it's not quite complete just yet. See my suggestions, let me know if something isn't clear how or why.
<application> | ||
<provider | ||
android:name=".ImageCropperFileProvider" | ||
android:authorities="${applicationId}.fileprovider" |
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
.
@@ -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 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.
import androidx.core.content.FileProvider; | ||
|
||
/** | ||
* Dummy file provider |
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.
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.
*/
@@ -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 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.)
Hey! I start a new project to handover this library The ideia is that we keep improving because this project don't have updates since Open to contribute, next pieces of work will be Android 11 permissions, refactor into Kotlin and ActivityContract |
hi Arthur, you're resued me, thanks a lot! , i've got folow question how , to delete some icon-buttons, and change color icon buttons ? |
🚨🚨🚨🚨🚨 THIS LIBRARY IS NOT MAINTAINED, PLEASE READ THIS 🚨🚨🚨🚨🚨 #818 |
This fixes #659, similar to expo/expo#3706, where a FileUriExposedException is thrown when the Image Cropper attempts to access a file created by the Camera.
Fix is adapted from expo/expo#3743.
A FileProvider is used to share the file securely using using a content:// uri.
A custom FileProvider is used rather than the default because of possible interactions when multiple authorities use the same FileProvider (https://stackoverflow.com/questions/43175014/possible-to-use-multiple-authorities-with-fileprovider)