在 phone 上为标准下载目录创建文件提供程序,以通过 Intent 发送 PDF 文件
Create a File Provider to the standard Download Directory on phone to send a PDF file via an Intent
当我将它传递给 PDF 查看器时 - 它出错并声明它无法打开该文件。
GoogleViewer 在日志中打印以下内容
2019-01-24 15:36:55.390 23756-23833/? V/LoadDocumentTask: Finish task: LoadDocumentTask(Display Data [PDF : mypdf.pdf] +ContentOpenable, uri: content://com.example.gregm.pdfplaygorund.provider/Download/Download/mypdf.pdf) result=FILE_ERROR time=16ms
发送意图的代码
String filepath = "storage/emulated/0/Download/mypdf.pdf";
File file = new File(filepath);
if(file.exists())
{
Log.e("ERRR", "file exists");
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> resInfoList = MainActivity.this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
MainActivity.this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(intent); // Crashes on this line
res/xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="."/>
</paths>
Androidmanifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.gregm.pdfplaygorund.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
在清单中包含这些逻辑还不够
我也必须请求许可
ActivityCompat.requestPermissions(ma,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(ma,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_READ_EXTERNAL_STORAGE);
那么上面的代码就可以工作了
当我将它传递给 PDF 查看器时 - 它出错并声明它无法打开该文件。 GoogleViewer 在日志中打印以下内容
2019-01-24 15:36:55.390 23756-23833/? V/LoadDocumentTask: Finish task: LoadDocumentTask(Display Data [PDF : mypdf.pdf] +ContentOpenable, uri: content://com.example.gregm.pdfplaygorund.provider/Download/Download/mypdf.pdf) result=FILE_ERROR time=16ms
发送意图的代码
String filepath = "storage/emulated/0/Download/mypdf.pdf";
File file = new File(filepath);
if(file.exists())
{
Log.e("ERRR", "file exists");
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> resInfoList = MainActivity.this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
MainActivity.this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(intent); // Crashes on this line
res/xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="."/>
</paths>
Androidmanifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.gregm.pdfplaygorund.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
在清单中包含这些逻辑还不够
我也必须请求许可
ActivityCompat.requestPermissions(ma,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(ma,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_READ_EXTERNAL_STORAGE);
那么上面的代码就可以工作了