-1

Hi I am new in android i have some PDF file that exist in this path asset/pdf/example.pdf

when i try PDF file with this code

Intent intent = new Intent(Intent.ACTION_VIEW);
    File file = new File("file:///android_asset/pdf/example.pdf");
    intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
    startActivity(intent);

i get this error & know it's because third party has no access to file what is solution?

Mohamad Amin
  • 93
  • 1
  • 7

1 Answers1

0

know it's because third party has no access to file

file:///android_asset/ only works for WebView AFAIK.

what is solution?

You can use my StreamProvider, which offers a ContentProvider that can serve a file from assets/.

Or, you can implement your own ContentProvider that can serve the file from assets/.

Or, you can copy the asset to a local file (e.g., in getFilesDir() or getCacheDir()), then use FileProvider to serve that local file.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • dude i read your GITHUB project too before ask question but cannot handle it again ! i new and don't knew how to use stream provider can you wrote base on my file path and give me example ? – Mohamad Amin Sep 17 '16 at 14:40
  • @MohamadAmin: [The `StreamProvider` `demo/` project](https://github.com/commonsguy/cwac-provider/tree/master/demo) demonstrates serving a file from assets, among other things. So do many of my book's drag-and-drop samples, such as [this one](https://github.com/commonsguy/cw-omnibus/tree/master/DragDrop/Legacy) (the `StreamProvider` is used in the `drag/` app module). – CommonsWare Sep 17 '16 at 14:48
  • i cannot handle it :( trying for two hours – Mohamad Amin Sep 17 '16 at 16:11