I am using a PDFViewer
library to view documents in an Activity
which is loaded from a given url
.
I am using the open source this library hosted on Git.
Everything works fine in the latest android version Nougat
& Oreo
. The file loads up and is shown in the screen just easy.
However, when testing with the older versions like Marshmallow
& Kitkat
, the document isn't loaded and I receive the following error
:
Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
My full Log report is:
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:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa2f736e0
D/Link:: http://mylink.com/FileUpload/Master/PDFModule/636586353416035750_सेफ हाउस संचालन सम्बन्धी सूचना.pdf
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:53)
at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
My PDFclass
code is as follows:
public void getPdfFile() {
pdf_link = this.getIntent().getExtras().getString("PDF_URL");
checkStrUrl = pdf_link.substring(pdf_link.lastIndexOf("_") + 1);
if (!checkFileInDevice(checkStrUrl)) {
new RetrievePDFStream().execute(pdf_link);
} else {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/Application Documents/" + checkStrUrl);
pdfView.fromFile(file).load();
}
}
class RetrievePDFStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}