1

I'm facing this error:

E/AndroidRuntime: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.mobgen.androidinterviewtest/files/LaFerrari.pdf
E/AndroidRuntime:     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
E/AndroidRuntime:     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
E/AndroidRuntime:     at com.mobgen.interview.SingleCarActivity$1.onClick(SingleCarActivity.java:92)

It's because of this line of code:

Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);

I have followed the resources that I had found: http://developer.android.com/reference/android/support/v4/content/FileProvider.html

Open file in cache directory with ACTION_VIEW

How to use support FileProvider for sharing content to other apps?

However I still couldn't manage to fix the error that I'm having.

Below you can see my code:

SingleCarActivity.java:

File file = new File(getFilesDir(), pdf); //pdf contains "LaFerrari.pdf"
Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);

AndroidManifest.xml:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="be.myapplication"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_pdf" path="assets/"/>
</paths>

Below you can see a screenshot of how my project structure looks like: Link: https://i.stack.imgur.com/oJ2mq.png

Community
  • 1
  • 1
superkytoz
  • 1,267
  • 4
  • 23
  • 43
  • `assets/` is a directory on your development machine. It is not a directory on the device. `FileProvider` cannot serve assets, only local files. Have you copied these files from assets to an `assets/` subdirectory under `getFilesDir()`? – CommonsWare Jan 31 '16 at 23:53
  • @CommonsWare I had googled it. Do you mean something like in the answer of the following question? link: http://stackoverflow.com/questions/8474821/how-to-get-the-android-path-string-to-a-file-on-assets-folder – superkytoz Jan 31 '16 at 23:57

1 Answers1

1

FileProvider cannot serve assets directly, as assets are not files. Your primary choices are:

  1. Copy the asset to a file, in a location that you have configured for FileProvider. I demonstrate that sort of FileProvider configuration in this sample app, but the answers to the question from your comment do the basics of copying the asset to a file.

  2. Use my StreamProvider, which can serve assets directly, without having to make a copy.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I had used the code from your sample app. But now I'm facing this new error: `No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://be.myapplication/my_pdf/LaFerrari.pdf flg=0x1 }` It's because of this line of code: `startActivity(i);` I have googled and I found this question: http://stackoverflow.com/questions/9157490/android-no-activity-found-to-handle-intent-error-how-it-will-resolve So I added a intent-filter to SingleCarActivity: http://pastebin.com/9jd2FACK But I still get the error. I have put the code on pastebin link: http://pastebin.com/NUNGxvcQ – superkytoz Feb 01 '16 at 21:02
  • @superkytoz: "But now I'm facing this new error" -- you need to have a PDF viewing app installed on your device, one that supports `content://` `Uri` values. Adobe Reader, Foxit, and ezPDF Reader all do, and others probably do as well. – CommonsWare Feb 01 '16 at 21:04
  • the pdf is blank. How to open pdf from assets. – Myat Min Soe Mar 18 '17 at 18:08
  • @septemberboy7: This is covered in my answer. If you are using one of those techniques, and it is not working for you, ask a separate Stack Overflow question, where you provide a [mcve]. – CommonsWare Mar 18 '17 at 18:10