Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing a pdf from internal storage throws file not found exception #2

Closed
shyamsam opened this issue Jun 9, 2016 · 11 comments
Closed

Comments

@shyamsam
Copy link

shyamsam commented Jun 9, 2016

Am trying a populate a pdf which is stored in internal storage is throwing FileNotFoundException.

    File file1 = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
    pdfView.fromFile(file1)
            .defaultPage(1)
            .onPageChange(this)
            .swipeVertical(true)
            .showMinimap(false)
            .load();

But when I manually check the directory, I could see that file exist. Or some times its giving me com.shockwave.pdfium.PdfOpenException: Open document failed in my log

@barteksc
Copy link
Collaborator

barteksc commented Jun 9, 2016

Environment.getExternalStorageDirectory() returns File, you have to call Environment.getExternalStorageDirectory().getAbsolutePath() to get String. Also, to read files from external storage you need <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> in your manifest

@ENOZA
Copy link

ENOZA commented Jun 9, 2016

barteksc, thank you for your PDF library for Android.
This is something I have been trying to find for a long time.

Since File.toString() usually returns the same value as File.getAbsolutePath(), it seems shyamsam's code is not a problem.

My code is almost the same as shyamsam's.

      File file = new File(Environment.getExternalStorageDirectory(), "pdf/sample.pdf");

      System.out.println("file.exists() = " + file.exists());

      pdfView.fromFile(file)
        .defaultPage(1)
        .showMinimap(false)
        .enableSwipe(true)
        .load();

file.exists() returns true but I also get the following error message when I try to load a PDF file on the internal memory of my device.

com.shockwave.pdfium.PdfOpenException: Open document failed

Of course, in my manifest, I put the following permission request.

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

Here is my stack trace.

com.shockwave.pdfium.PdfOpenException: Open document failed
     at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:72)
     at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:85)
     at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:37)
     at android.os.AsyncTask$2.call(AsyncTask.java:288)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     at java.lang.Thread.run(Thread.java:841)

When I use fromAsset(), it works perfectly.
Only when I use fromFile() it gives the error as shown above.

Thank you for checking.

@barteksc
Copy link
Collaborator

barteksc commented Jun 9, 2016

@ENOZA You are right with File.toString.
Does System.out.println("file.exists() = " + file.exists()); show true? I see that File.toString() returns path without trailing slash, so it is necessary to write "/pdf/sample.pdf" instead of "pdf/sample.pdf".

@ENOZA
Copy link

ENOZA commented Jun 9, 2016

Yeah, System.out.println("file.exists() = " + file.exists()); shows true. I edited my comment to mention that.

@barteksc
Copy link
Collaborator

barteksc commented Jun 9, 2016

@ENOZA Sorry, I didn't notice that you are using 2nd argument of File constructor. Unfortunately I can't reproduce this behavior on 3 devices, maybe it's something bigger. You get exception from Pdfium, so maybe it has problem with opening your file, could you try with another one? I don't have any other ideas now.

@ENOZA
Copy link

ENOZA commented Jun 10, 2016

barteksc, thank you very much and I have to I apologize to you.
It seems it was because of my Android file system.
I copied a PDF file to my device by connecting it to a computer by a USB cable.
When I checked the file size on the device I realized it was zero.... (It existed there but it was an empty file).
I am sorry for my not noticing it.
When I disconnected the USB cable, the file size was restored to the original size and AndroidPdfViewer perfectly render the PDF file!

@shyamsam
Copy link
Author

shyamsam commented Jun 10, 2016

@barteksc thanks for you reply,

am still facing the same issue as @ENOZA has mentioned in earlier comments. I have given the permission in the manifest.

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

Am downloading the file from a remote server to a folder in phone storage. If I go to that particular directory I could see that file exists there and am able to open that file with other pdf viewers. The error an getting is when an populating the pdf. The log is shown below:

com.shockwave.pdfium.PdfOpenException: Open document failed
at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:72)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:61)
at    com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:32)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

@barteksc
Copy link
Collaborator

@shyamsam please check if version 1.1.0 fixes issue

@shyamsam
Copy link
Author

Hi @barteksc ,

Thanks for the update. But am sorry to say that am still facing some issues,
As per the latest library am trying to load the pdf from a remote server. There am getting File not Found Exception. But when I load the same pdf url in browser is works. The logcat is shown below:

load pdf error
java.io.FileNotFoundException: No content provider:http://www.gnfiliaton.gr/sites/default/files/20131231103232738561744.pdf
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:959)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:816)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:771)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.getSeekableFileDescriptor(DecodingAsyncTask.java:81)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:60)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:31)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

But when I download the file to any local folder and calling pdfView.fromFile(), it works like charm. Thanks

@barteksc
Copy link
Collaborator

Library doesn't support remote content, so it works as expected. Glad that update fixed loading from internal memory.

@kondasusmithareddy
Copy link

Pdf is not working in Kitkat version.When I Click on link,It Displaying Blank Page in My Emulator,Can u help me Please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants