I have been trying to solve a problen with sharing image from external storage directory. it works in most of the devices, but It is not working in others. I have: 1. Added a class extending FileProvider:
public class GenericFileProvider extends FileProvider {}
- Added a FileProvider tag in AndroidManifest.xml under tag:
<provider
android:name=".Utils.GenericFileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
- created a provider_paths.xml file in res/xml folder:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="MissingDefaultResource">
<external-path name="external_files" path="."/>
</paths>
and my function for sharing image is down below:
private fun shareInEmail() {
val filename = "Avoir+$timeStamp.jpg"
val filelocation = File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename)
val path : Uri = FileProvider.getUriForFile(
requireContext(),
context!!.applicationContext.packageName.toString() + ".provider",
filelocation
)
context!!.grantUriPermission(
"com.ideasfactory.mjcprojet",
path,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// set the type to 'email'
emailIntent.type = "vnd.android.cursor.dir/email"
emailIntent.setType("text/plain")
emailIntent.type = "application/jpg"
val to = arrayOf("contact@mjclambres.fr")
emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
emailIntent.putExtra(Intent.EXTRA_BCC, arrayOf<String>())
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, path)
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Transaction")
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(Intent.createChooser(emailIntent, "Transaction"))
}
All the previous steps reference to this answer: [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
I don’t know what i am missing? My log says:
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.ideasfactory.mjcprojet.Utils.GenericFileProvider uri content://com.ideasfactory.mjcprojet.provider/external_files/Avoir%2B2020-06-11%2011%3A08.jpg from pid=12602, uid=1000 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:593)
at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:507)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:307)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
I will appreciated all your help