android.os.FileUriExposedException生成下载pdf时出错

时间:2019-02-05 09:28:10

标签: android

无法生成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);
        }

1 个答案:

答案 0 :(得分:0)

当您的targetSdk版本> = 28时发生 因此,请在onCreate()方法的活动/片段中使用此行来忽略uri暴露

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

`