int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
} else {
File file = new File("/sdcard/reportSituationAddress.pdf");
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider" ,file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(Pdftest.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(Pdftest.this, "not found", Toast.LENGTH_LONG).show();
}
我有一个pdf文件,我需要单击一个按钮打开它。这样,单击后用户可以选择应用程序来打开文件,然后在其中打开文件。我使用提供程序,但是不断出错。请帮助解决问题。
它是我的文件提供者
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/telros.brigade/files/Pictures" />
<external-files-path name="name" path="AisBrigade/Pdf" />
</paths>
这是我的清单
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="ru.npobaltros.aisbrigade.mapsdk.storage.workmanager-init_new"
android:enabled="false"
android:exported="false"
tools:replace="android:authorities" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="telros.brigade"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
应该是这样,只有打开pdf的程序。 enter image description here
我的错误 enter image description here
也许有人在执行该代码的地方,如果可以的话,请显示
答案 0 :(得分:0)
您的FileProvider
元数据中没有覆盖所选路径的条目。
首先,停止对路径进行硬编码。使用方法从根位置汇编路径。我的猜测是您认为您的PDF位于external storage的根目录中。在这种情况下,您将使用:
File file = new File(Environment.getExternalStorageDirectory(), "reportSituationAddress.pdf");
然后,确保您的FileProvider
元数据支持您的根位置。对于Environment.getExternalStorageDirectory()
,use <external-path>
。
此外,请注意you have very little filesystem access on Android 10 and higher。例如,Environment.getExternalStorageDirectory()
在Android 10上已被弃用,因为默认情况下您无权使用它。