Intent.EXTRA_STREAM requires the provider be exported, or grantUriPermission()

/**
     * 分享文件到系统分享
     * @param context
     * @param file
     */
    public void shareFile(Context context,String file){
//        File externalStorageDirectory = Environment.getExternalStorageDirectory();

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
//                shareIntent.setPackage("com.android.bluetooth");
        Uri uriForFile = JiZhiFileProvider.getUriForFile(context, "com.iflytek.jzapp.fileProvider", new File(file));
        shareIntent.putExtra(Intent.EXTRA_STREAM,
                uriForFile);
        shareIntent.setType("*/*");//此处可发送多种文件
        //必须这么写×××××××××××
        Intent chooser = Intent.createChooser(shareIntent, "Share File");
//核心代码×××××××××××××××××××××××××××
        List resInfoList = context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uriForFile, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        context.startActivity(chooser);
    }

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

问题描述

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

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 = 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 = 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("[email protected]")
    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,则授予权限.但仍然无法正常工作

我使用 FileProvider 来共享内容.

这是我的清单:

 
            
        

这是我的provider_paths.xml文件



    

这是日志:

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)

你可能感兴趣的:(android,java,android,studio)