0

I am using PDF Viewer Library to view PDF inside an Android app. I've browsed through a lot of tutorials, one of which is Dipak's tutorial. I want to access a PDF file stored in my assets folder instead of accessing the external storage. My problem is, I can't get the "path" right. It always return file not found.

I've tried the following, still yields the same result:

  • this.getAssets()
  • file:///android_assets/file_name.pdf
  • file:///android_asset/file_name.pdf
Community
  • 1
  • 1
Yukai
  • 43
  • 9

1 Answers1

1

You can't get path from asset, have to right in sd or internal memory to get the path.

For SD Card

First Take Permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

then write it in card

 File f = new File(Environment.getExternalStorageDirectory() + "file.pdf");
  if (!f.exists()) try {

    InputStream is = getAssets().open("file.pdf");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();


    FileOutputStream fos = new FileOutputStream(f);
    fos.write(buffer);
    fos.close();
  } catch (Exception e) { throw new RuntimeException(e); }

  Staring path = f.getPath(); 
Chinmoy Debnath
  • 2,814
  • 16
  • 20
  • `File f = new File("Directory Where you want to place " +"/file.pdf");` will return file not found. since the file hasn't been written to the directory – Yukai Jul 21 '13 at 15:29
  • visit this link to learn how to write a file in SD card http://stackoverflow.com/questions/8181242/write-to-a-file-in-sd-card-in-android – Chinmoy Debnath Jul 21 '13 at 15:36
  • tried this `File f = Environment.getRootDirectory();` still file not found >_ – Yukai Jul 21 '13 at 15:36