2

I am trying to show pdf file in my application. I am using Pdf Viewer library(https://github.com/barteksc/AndroidPdfViewer). I have put a pdf file in my assets folder. But when I try to load the file it shows this exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samsung.secautomation/com.samsung.secautomation.ui.activities.ShowPdfActivity}: com.github.barteksc.pdfviewer.exception.FileNotFoundException: src/main/assets/test does not exist

Here is my code:

com.github.barteksc.pdfviewer.PDFView pdfView=(com.github.barteksc.pdfviewer.PDFView) findViewById(R.id.pdfViewer);
    pdfView.fromAsset("src/main/assets/test") //test is the pdf file name
            .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
            .enableSwipe(true)
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(0)
            .enableAnnotationRendering(false)
            .password(null)
            .scrollHandle(null)
            .load();

Here is my permission in Manifest file:

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

I am using Ubuntu 12.04

Is there any way to solve this problem.

Thank you

supertramp
  • 39
  • 5
  • Can you post some more details?? For example the Project structure. I think the file path is wrong... maybe try to add ".pdf" to the path... –  Oct 31 '16 at 10:52
  • 2
    Also looks like you shouldn't put this 'src/main/assets/test' path. try: pdfView.fromAsset("test.pdf"). Inside the PDFView.fromAsset 'context.getAssets().open()' is used http://stackoverflow.com/a/5771369/1533933 so you should not write path from your source code folder – krossovochkin Oct 31 '16 at 10:56
  • `src/main/assets/test` ? if file on compile machine is not in `.....assets/src/main/assets/` and is not called `test` without ext. then `FileNotFoundException is obvious ... – Selvin Oct 31 '16 at 10:56
  • pdfView.fromAsset() take only String as parameter thats why I put the file location. The application structure is Project-->app-->src-->main-->assets-->test.pdf – supertramp Oct 31 '16 at 11:09
  • assets are in a "pre-defined" location, and do not refer to your build path. same is true for all the Android asset files. – escape-llc Oct 31 '16 at 11:23
  • none of the given permissions are required to read your own files, e.g. assets. – escape-llc Oct 31 '16 at 11:24

1 Answers1

2

Replace src/main/assets/test with test. src/main/assets/test is a relative path to this file on your development machine, not in the assets as packaged on the device.

Note that I assume that you manually removed the .pdf extension from the file when you put it in src/main/assets/. If that file still has the .pdf extension, you probably need it in your code. Resources drop their extensions; assets do not.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491