问题
I have been trying to share one image from external storage and it is not working in API 29. the error message is: Permission denial, the file requires the provider be exported, or grantUriPermission(). when I google it it seems to be a problem to old versions. I don't know why i’m having this problem with 29 API. I have used all the solutions related to grantUriPermission(), as you can see in my method:
private fun shareInEmail() {
val filename = "Avoir+$timeStamp.jpg"
val filelocation =
File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename)
//val path = Uri.fromFile(filelocation)
val path: Uri = FileProvider.getUriForFile(
requireContext(),
context!!.applicationContext.packageName + ".provider",
filelocation
)
val emailIntent = Intent(Intent.ACTION_SEND)
//if current version is greater than 21 version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
} else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {// if current version is equal to 29 version
//grant permision for app with package to all
val resInfoList: List<ResolveInfo> = requireContext().packageManager
.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName: String = resolveInfo.activityInfo.packageName
requireContext().grantUriPermission(
packageName,
path,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
}else {//lower versions
val resInfoList: List<ResolveInfo> = requireContext().packageManager
.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) {
val packageName: String = resolveInfo.activityInfo.packageName
requireContext().grantUriPermission(
packageName,
path,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
}
//emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// set the type to 'email'
emailIntent.type = "vnd.android.cursor.dir/email"
val to = arrayOf("contact@mjclambres.fr")
emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
// the attachment
emailIntent.putExtra(Intent.EXTRA_STREAM, path)
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Transaction")
if (emailIntent.resolveActivity(context!!.getPackageManager()) != null) {
startActivity(Intent.createChooser(emailIntent, "Transaction"))
}
}
I specified, if is API is 29, granted permission. but still not working
For sharing content I used FileProvider.
this is my Manifest:
<provider
android:name="androidx.core.content.FileProvider"
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>
this is my provider_paths.xml file
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
and this are the logs:
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.ideasfactory.mjcprojet.provider/.provider/Avoir%2B2020-06-11%2019%3A16.jpg from pid=15561, 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 have been in this for a wyle, I don't know what i'm missing. I will appreciate all your help.
回答1:
After searchig for hours and hours, I finally find a solution based on enter link description here. I had to add to manifest this line : android:requestLegacyExternalStorage="true"
to make content redeable in version 10 , Q API 29, and everithing works.I could attach the file.
来源:https://stackoverflow.com/questions/62332302/permission-denial-coulnt-attach-file-file-requires-the-provider-be-exported