PDF load error in com.github.barteksc.pdfviewer

1k Views Asked by At

I'm using com.github.barteksc.pdfviewer library to view pdf files. I don't know what happened it stopped working from past 2 weeks, Im getting PDF load error.

This is my PdfViewer.java

class RetrievePdfStream extends AsyncTask<String, Integer, InputStream> {

        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            int count;
                try {
                     URL url = new URL(strings[0]);
                     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                     int lenghtOfFile = urlConnection.getContentLength();
                        if (urlConnection.getResponseCode() == 200) {
                          inputStream = new BufferedInputStream(urlConnection.getInputStream());
                         }    
                          byte data[] = new byte[1024];
                          long total = 0;

                     while ((count = inputStream.read(data)) != -1) {
                          total += count;
                          // publishing the progress....
                          // After this onProgressUpdate will be called
                         // publishProgress(""+(int)((total*100)/lenghtOfFile));
                         publishProgress((int)((total*100)/lenghtOfFile));
                     }

                    if(inputStream != null)
                     inputStream.close();    
                }
                catch (IOException e) {
                      Log.e("Error: ", e.getMessage());
                }

            return inputStream;
        }

        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        protected void onPostExecute(InputStream inputStream) {
            pdf.fromStream(inputStream).load();
         //   imageView.setVisibility(View.INVISIBLE);
            pDialog.cancel();
        }
    }

Logcat is showing this error

    2022-03-20 09:16:01.286 19257-19257/ak.wp.meto E/SurfaceFlinger: resetPartialBlurMask failed to transact: -1
    2022-03-20 09:03:08.207 12789-12789/ak.wp.meto E/PDFView: load pdf error
        java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
            at com.github.barteksc.pdfviewer.util.Util.toByteArray(Util.java:36)
            at com.github.barteksc.pdfviewer.source.InputStreamSource.createDocument(InputStreamSource.java:37)
            at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:49)
            at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
            at android.os.AsyncTask$3.call(AsyncTask.java:394)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
            at java.lang.Thread.run(Thread.java:923)

This code worked fine before. I don't know what went wrong. How to fix it?

AndroidManifest.xml

 <!-- storage permission -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
1

There are 1 best solutions below

0
On

Try this code snippet

try {
  new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... voids) {
      try {
        InputStream input = new URL(uri).openStream();
        pdfview.fromStream(input).load();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null;
    }
  }.execute();
} catch (Exception e) {
  e.printStackTrace();
}