权限拒绝尝试将csv文件发送到电子邮件时,要求导出提供程序,或granturipermission()

2ic8powd  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(174)

我正在从firebase中提取的数据生成一个csv文件,然后尝试将其附加到电子邮件中(大约两周前,这段代码还有效!),但是,当我现在执行此任务时,在我的日志cat中不断出现相同的错误,即保存文件无法附加的toast消息以及以下错误:

2020-08-20 17:05:43.355 11147-11268/com.example.cpd E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.cpd.provider/external/MyCPDAudit.csv from pid=30180, uid=1000 requires the provider be exported, or grantUriPermission()

我生成电子邮件意图的代码如下

//INTENT TO SEND USER EMAIL
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            //SETS THE USERS EMAIL AS THE RECIPIENT EMAIL ADDRESS
            emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{user.getEmail()});
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Audit Profile");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Thank you for using CPD Journal to build your CPD Audit Profile. Please see the attached file for your records.");
            emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            File file = new File(csv);
            Uri uri = FileProvider.getUriForFile(SavedAudit.this, "com.example.cpd.provider", file);
            emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

            //USER CAN SELECT HOW THEY WANT TO SHARE THE CSV FILE
            startActivity(Intent.createChooser(emailIntent, "Pick an email provider"));

我在android清单中拥有以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

清单中的文件提供程序是

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

我的文件路径是

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

根据logcat,我已经尝试在我的清单文件中导出提供者,但这会使应用程序在启动时崩溃。
我尝试添加以下两行与我的电子邮件意图,但这会导致相同的日志猫错误:

String packageName = getApplicationContext().getPackageName();
grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

我的csv和uri日志如下

2020-08-20 18:03:30.969 20914-20914/com.example.cpd D/TAG: csv = /storage/emulated/0/MyCPDAudit.csv
2020-08-20 18:03:30.974 20914-20914/com.example.cpd D/TAG: uri = content://com.example.cpd.provider/external/MyCPDAudit.csv

但是,我不确定这样调用是否正确地使用了这个granturipermission方法-有什么办法解决这个问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题