权限被拒绝:不能附加文件,文件要求导出提供程序,或grantUriPermission(),API 29 [英] Permission Denial: Couln't attach file, File requires the provider be exported, or grantUriPermission(), API 29

查看:1583
本文介绍了权限被拒绝:不能附加文件,文件要求导出提供程序,或grantUriPermission(),API 29的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从外部存储共享一个图像,但它在API 29中不起作用.错误消息是:权限被拒绝,该文件要求提供者被导出,或grantUriPermission().当我用google搜索时,旧版本似乎是个问题.我不知道为什么我在使用29 API时会遇到此问题. 正如您在我的方法中看到的那样,我已经使用了与grantUriPermission()相关的所有解决方案:

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"))
    }

}

我指定了,如果API是29,则授予权限.但仍然无法正常工作

I specified, if is API is 29, granted permission. but still not working

我使用 FileProvider 来共享内容.

这是我的清单:

 <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>

这是我的provider_paths.xml文件

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>

这是日志:

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.

推荐答案

经过数小时的搜索,我终于找到了基于在此处输入链接描述.我必须添加以下行以表示这行:android:requestLegacyExternalStorage="true"以使内容在版本10,Q API 29和所有版本中均可重做.我可以附加文件.

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.

这篇关于权限被拒绝:不能附加文件,文件要求导出提供程序,或grantUriPermission(),API 29的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆