android 在com.github.barteksc.pdfviewer中出现PDF加载错误

bweufnob  于 2022-11-27  发布在  Android
关注(0)|答案(1)|浏览(657)

我正在使用com。github。barteksc。pdfviewer库查看PDF文件。我不知道发生了什么事,它停止工作,从过去2个星期,我得到PDF加载错误。
这是我的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显示此错误

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)

这段代码以前运行得很好。我不知道哪里出了问题。如何修复它?

安卓清单.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"/>
py49o6xq

py49o6xq1#

尝试以下代码片段

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();
}

相关问题