问题描述
我正在从Firebase提取的数据中生成CSV文件,然后尝试将其附加到电子邮件(大约两周前,此代码起作用了!),但是我仍然收到与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,我曾尝试在Manifest文件中导出提供程序,但这会在启动时使应用程序崩溃。
我尝试在电子邮件意图中添加以下两行,但这会导致相同的日志猫错误:
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方法-任何解决此问题的想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)