【发布时间】:2020-04-09 09:50:25
【问题描述】:
嗨,在 pdf 代码中,我有一个按钮查看 pdf。当单击 pdf 时,我以字符串 url 的形式访问文件,然后想要打开 pdf 文件。但是当我尝试单击下面的按钮时问题。来自外部目录
谁能帮我解决这个问题。
pdf:
holder.pdf.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory(),
opportunity.getPdf_link());
Uri path = Uri.fromFile(file);
Uri pdf = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(pdf, "application/pdf");
try {
mContext.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
// handle no application here....
}
}
});
在清单中:
<provider
android:name="androidx.core.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>
android.os.FileUriExposedException: file:///storage/emulated/0/https%3A/www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
at android.net.Uri.checkFileUriExposed(Uri.java:2371)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10247)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4544)
【问题讨论】:
-
您的堆栈跟踪与您的代码不匹配。您的堆栈跟踪显示对
startActivityForResult()的调用,而您的代码显示对startActivity()的调用。 -
你能解释一下我不明白...
-
@CommonsWare 无法访问此文件。请检查位置或网络,然后重试我现在面临的这个问题