Piotr
Piotr

Reputation: 3970

Share PDF from assets. FileUriExposedException

I want to open PDF, which is in interaly saved in app/assets/pdf_name.pdf

I get the file using File f = new File(getContext().getCacheDir() + /pdf_name.pdf");

I'm trying to extract URI if form of content:// using

Uri pdfUri = FileProvider.getUriForFile(getContext(), "pcg_name.fileprovider", file);

but I got

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/pck_name/cache/pdf_name.pdf

ALthout I've configurated

<paths>
    <cache-path
        name="my_documents"
        path="assets/"/>
    <cache-path
        name="my_pdf"
        path="cache/"/>
</paths>

My goal is to open PDF via Intent, but I can not do just:

Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(pdfFile),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
target.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // DON'T FORGET this

Because it throws File Uri exposed exception

Upvotes: 1

Views: 587

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

I get the file using

That code has nothing to do with assets. Assets are files on your development machine; they are not files on the device, unless you manually copy the content to a file.

Perhaps you have code, not shown in the question, where you copied the file from assets to new File(getContext().getCacheDir() + /pdf_name.pdf").

ALthout I've configurated

Neither of those are valid for new File(getContext().getCacheDir() + /pdf_name.pdf"). Delete one <cache-path> element and get rid of the path attribute from the other <cache-path> element, leaving you with:

<paths>
  <cache-path name="my_pdf"/>
</paths>

Upvotes: 1

Related Questions