【发布时间】:2019-10-06 12:44:42
【问题描述】:
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的程序。
我的错误
也许有人有一段工作代码,在哪里实现,如果有,请展示
【问题讨论】:
-
嘿,你有没有找到解决方案。遇到同样的问题,似乎无法找到关于该主题的直接答案。