【问题标题】:android.os.FileUriExposedException Error while generating download pdfandroid.os.FileUriExposedException 生成下载pdf时出错
【发布时间】: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


【解决方案1】:

当您的 targetSdk 版本 >=28 时会发生这种情况 因此,在 onCreate() 方法中的 activty/fragment 中使用此行来忽略 uri 暴露

` StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

`

【讨论】:

  • 是的,这不是一个最佳解决方案,最好的解决方案是创建一个自定义文件提供程序类...但是当您需要一些快速破解时..那么这种方法就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多