使用 Android 意图打开 PDF 文件
Opening PDF file using Android intent
场景是这样的:
我正在从网络下载 PDF 并将其保存在 /0/Download/NSIT - Notices/"fileName".pdf
现在像这样发送 Intent:
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 reader(Google 驱动 PDF Reader,系统 PDF 查看器)都在说
The File Does Not Exist
图片:
Google 一个坐着什么都不做(这就是为什么不包括它的图像)
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>
我正在 AsyncTask 中创建文件,但从该 AsyncTask 的 postExecute 打开
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");
Logcat
之前它抛出 FileUriExposedException
然后我修复了这个 FileProvider
问题。问题是我的应用程序下载的 pdf 文件已成功创建,我可以从任何文件管理器(现在是 ES 文件资源管理器)成功打开它,但不能从我的应用程序:(。我是 FileProvider
的新手。需要求助!
PS: 那也不是我的设备问题。测试了另外两部具有不同 ROM 和 API >= 23
的手机
请参阅 File provider
的文档
Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application
can use the provider's content URI to access it, subject to the
permissions specified for the provider.
false: The provider is not available to other applications.
access to the provider to your applications. Only applications that
have the same user ID (UID) as the provider will have access to it.
尝试改变android:exported="true"
请设置intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
它会将您的文件的访问权限授予其他提供商
场景是这样的:
我正在从网络下载 PDF 并将其保存在 /0/Download/NSIT - Notices/"fileName".pdf
现在像这样发送 Intent:
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 reader(Google 驱动 PDF Reader,系统 PDF 查看器)都在说
The File Does Not Exist
图片:
Google 一个坐着什么都不做(这就是为什么不包括它的图像)
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>
我正在 AsyncTask 中创建文件,但从该 AsyncTask 的 postExecute 打开
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");
Logcat
之前它抛出 FileUriExposedException
然后我修复了这个 FileProvider
问题。问题是我的应用程序下载的 pdf 文件已成功创建,我可以从任何文件管理器(现在是 ES 文件资源管理器)成功打开它,但不能从我的应用程序:(。我是 FileProvider
的新手。需要求助!
PS: 那也不是我的设备问题。测试了另外两部具有不同 ROM 和 API >= 23
的手机请参阅 File provider
的文档Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application can use the provider's content URI to access it, subject to the permissions specified for the provider.
false: The provider is not available to other applications. access to the provider to your applications. Only applications that have the same user ID (UID) as the provider will have access to it.
尝试改变android:exported="true"
请设置intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
它会将您的文件的访问权限授予其他提供商