【发布时间】:2019-06-29 01:58:07
【问题描述】:
无法生成 pdf 下载,出现 android.os.FileUriExposedException 错误:file:///storage/emulated/0/Download/INV-0002.pdf 通过 Intent.getData() 暴露在应用之外
String path = downloadPDF();
if (path.length() == 0) {
Toast.makeText(NewInvoiceActivity.this, getResources().getString(R.string.pdf_not_created), Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
【问题讨论】:
-
因为您的代码中缺少 FileProvider。欲了解更多信息check
-
字符串路径 = downloadPDF(); Uri pdfURI = FileProvider.getUriForFile(getApplicationContext(), "in.khushiuka.invoicing", new File(path)); if (path.length() == 0) { Toast.makeText(NewInvoiceActivity.this, getResources().getString(R.string.pdf_not_created), Toast.LENGTH_SHORT).show(); } else { Intent 意图 = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(pdfURI, "application/pdf");意图.setFlags(意图.FLAG_ACTIVITY_NO_HISTORY);开始活动(意图);
-
出现错误:java.lang.NullPointerException:尝试调用虚拟方法 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java. lang.String)' 在空对象引用上
标签: android