0

I've a little content provider to open a simple pdf in my app package with an external application, but whe the open() run the parcelfiledescription return me a FileNotFoundException.

I don't understand what is the right sintax to give the correct file path to the parcel descriptor...

    public ParcelFileDescriptor openFile(Uri uri, String mode) {
            Log.i("info","eseguo providing");
            URI uri1 = URI.create("file:///data/data/package.name/assets/prova.pdf");
            File file = new File(uri1);
            ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);        
            return parcel;
}

Thanks for any help!


i've tried this simple code:

URI uri1 = URI.create("file:///android_asset/prova.pdf");
File file = new File(uri1);
Log.i("info","file exist: " + file.exists());

but it return ever false!

Marco Faion
  • 607
  • 3
  • 14
  • 23

1 Answers1

0

From [ParcelFileDescriptor.open method][1] (bold emphasis mine)

FileNotFoundException Throws FileNotFoundException if the given file does not exist or can not be opened with the requested mode.

Have you tried changing the MODE to MODE_READ_ONLY and see if that works?

[1]: http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#open(java.io.File, int)

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • What are the values for the following, assuming the variables from your question: `file.exists()`, `file.canRead()`, `file.canWrite()`. Do all of those expressions return `true`? – typo.pl Mar 08 '11 at 10:37
  • false, false and... false, i think that the assets folder is not readeble from the content provider... in my project the assets folder contain the file, in another activity of my app i can open correctly an image contained in the assets folder with webview.loadurl("file:///android_asset/image.jpg"); but if i try to open the file with File file = new File("file:///android_asset/prova.pdf"); i get false, false and false – Marco Faion Mar 08 '11 at 10:54
  • 1
    Looks like you can't use File APIs to access data in `assets/`. From : Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager. – typo.pl Mar 08 '11 at 16:20
  • 1
    The naswer here is a better explanation of the problem with `assets/` and File APIs: – typo.pl Mar 08 '11 at 16:21
  • great! i think this is the right way, i'm tring to implement it, but i'm not able to convert the inputstream returned by the AssetManager to a File object... i need this in order to provide it to the ParcelFileDescriptor that the content provider must return... – Marco Faion Mar 09 '11 at 08:43