使用Android意图打开PDF文件
下面是一个场景: 我正在从网络下载PDF,并将其保存在使用Android意图打开PDF文件,android,pdf,android-intent,android-fileprovider,android-storage,Android,Pdf,Android Intent,Android Fileprovider,Android Storage,下面是一个场景: 我正在从网络下载PDF,并将其保存在/0/Download/NSIT-Notices/“fileName.PDF”中 现在,像这样发送意图: private void openPDF(String fileName) { Log.d(TAG, "openPDF: called"); Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory
/0/Download/NSIT-Notices/“fileName.PDF”中
现在,像这样发送意图:
private void openPDF(String fileName) {
Log.d(TAG, "openPDF: called");
Intent intent = new Intent(Intent.ACTION_VIEW);File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(Environment.getExternalStorageDirectory() + "/Download/NSIT - Notices", fileName + ".pdf");
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", myPDF);
Log.d(TAG, "openPDF: intent with uri: " + uri);
intent.setDataAndType(uri, "application/pdf");
context.startActivity(Intent.createChooser(intent, "Open with..."));
}
现在任何PDF阅读器(谷歌驱动PDF阅读器、系统PDF阅读器)都在说
The File Does Not Exist
图片:
谷歌一号坐着什么都不做(这就是为什么不包括它的图片)
Logcat
之前它抛出了FileUriExposedException
,然后我修复了这个FileProvider
问题。问题是,我的应用程序下载的pdf文件已成功创建,我可以从任何文件管理器(现在是ES文件资源管理器)成功打开它,但无法从我的应用程序打开它:(.我是文件提供商的新手。
。需要帮助
PS:这也不是我的设备问题。用另外两部具有不同ROM和API>=23的手机测试。有关详细信息,请参阅文档
内容提供商是否可供其他应用程序使用:
true:提供程序可用于其他应用程序。任何应用程序
根据
为提供程序指定的权限
false:该提供程序对其他应用程序不可用。
访问应用程序的提供程序。只有
与提供程序将有权访问的用户ID(UID)相同
尝试更改android:exported=“true”请设置intent.setFlags(intent.FLAG\u GRANT\u READ\u URI\u PERMISSION);
它会将您的文件的访问权授予其他提供商我认为它必须与FileProviderset this before startActivityintent.setFlags(intent.FLAG\u grant\u READ\u URI\u权限)做些什么
在实现FileProvider以克服FileUriExposedException之前,我已经尝试过这个方法。没有帮助。但是现在实现了File Provider并添加了这个意图标志。Wokring Now。谢谢伙计。@Sunny Post回答,我会接受它。谢谢!:)您的欢迎很高兴看到它工作:)java.lang.RuntimeException:无法获取提供程序android.support.v4.content.FileProvider:java.lang.SecurityException:不能导出提供程序
Mainfest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download", "NSIT - Notices");
File myPDF = new File(myDir, params[1] + ".pdf");
if (!myDir.exists())
myDir.mkdirs();
try {
fileOutputStream = new FileOutputStream(myPDF);
fileOutputStream.write(response.bodyAsBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "doInBackground: Download complete");