0

Not able to generate pdf download getting error of android.os.FileUriExposedException: file:///storage/emulated/0/Download/INV-0002.pdf exposed beyond app through 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);
        }
Khushiuka
  • 11
  • 1
  • 5
  • Because of missing _FileProvider_ in your code. For more info [check](https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0) – Piyush Feb 05 '19 at 09:29
  • try this answer https://stackoverflow.com/a/38858040/9060917 – Praveen Feb 05 '19 at 09:32
  • See : [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed#answer-38858040) – Kévin Giacomino Feb 05 '19 at 09:40
  • String path = 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 intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(pdfURI, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); – Khushiuka Feb 05 '19 at 10:37
  • Getting error : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference – Khushiuka Feb 05 '19 at 10:37

1 Answers1

-1

This occurs when your targetSdk version >=28 So use this lines in your activty/fragment in the onCreate() method to ignore the uri exposure

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

`

Ashmeet Arora
  • 164
  • 1
  • 3
  • 11
  • yes it is not an optiamal solution, the best solution is to create a custom file provider class... But when you need some quick hack..then this method works like a charm ! – Ashmeet Arora Feb 05 '19 at 09:41