文件共享提供者未授予权限

时间:2019-10-13 05:23:46

标签: java android android-fileprovider

正在尝试共享我已下载的文件。在下载过程中,将文件保存为

 String filename = Environment.getExternalStorageDirectory() + "/Download/" + "ChecklistItems" + System.currentTimeMillis() + ".pdf";
 OutputStream stream = new FileOutputStream(filename);
  stream.write(responseImage);
 stream.close();

stream.close开始成功保存后,SO开始通过共享文件

     Uri path = FileProvider.getUriForFile(this, getResources().getString(R.string.site_package_name), new File(filename));

     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.putExtra(Intent.EXTRA_TEXT, site_name+" inspection checklist");
     shareIntent.putExtra(Intent.EXTRA_STREAM, path);
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     shareIntent.setType("application/pdf");
     startActivity(Intent.createChooser(shareIntent, "Share..."));

我的包裹名称是com.soradius.rty

我在清单中添加了

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.soradius.rty"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

我将provider_paths添加为

<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="external_files" path="."/>
 <files-path name="downloads_path" path="." />

但是在共享过程中会出错

java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider 

 uri content://com.soradius.rty/external_files/Download/ChecklistItems1570943693555.pdf
 from pid=29647, uid=1000 requires the provider be exported, or grantUriPermission()

我缺少什么,因为我希望从downloads文件夹中获取文件并共享。

0 个答案:

没有答案