I am using build variants with Firebase and my application also needs to use FileProvider for sharing app files to be opened in 3rd party apps. But when I am trying to send the URI in the intent, the app crashes giving following error.
error:java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.debug/files/Pictures/JPEG_20170919_113955_102027655.jpg
Heres my configuration
String authority = getString(R.string.authority_provider);
Uri fileUri = FileProvider.getUriForFile(context, authority, file);
Added file provider into AndroidManifest.xml at inside of application tag:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="@string/authority_provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="my_images" path="Android/data/${applicationId}/files/Pictures" /> <external-file-path name="my_images" path="Android/data/${applicationId}/files/Pictures" /> </paths>
As Firebase
mandates using 2 application IDs to keep crash logs, analytics separate in Firebase Console
String: authority_provider - added into build.gradle(app):
buildTypes { release { --- applicationIdSuffix "com.example" resValue "string", "authority_provider", "\"com.example.fileprovider\"" --- } debug { --- applicationIdSuffix '.debug' resValue "string", "authority_provider", "\"com.example.debug.fileprovider\"" --- } }
File: and creating file into ExternalFilesDir and saving downloaded data into it and using same file object to get URI.
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); File file = File.createTempFile(fileName, fileExtension, storageDir);
I have tryed following solution to solve but not worked for me:
- https://stackoverflow.com/a/42555624/8240915
- https://stackoverflow.com/a/42516202/8240915
- https://stackoverflow.com/a/40463201/8240915
- https://stackoverflow.com/a/21989153/8240915
Also followed this blog post.