问题
I have have uploaded pdf files and set it to the database Stored steps in Firebase Database and i have create a code to display in android and it works fine Displaying in listview
i have create an intent to open in a new page and view the pdf in android
public class ViewPdfFiles extends AppCompatActivity {
private PDFView pdfView;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pdf_files);
pdfView=(PDFView) findViewById(R.id.pdfView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
url = getIntent().getStringExtra("url");
}
new RetrievePDFStream().execute(url);
}
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();
}
}
}
But it was giving an error
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:333)
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:764)
what's the error and how can i resolve it
来源:https://stackoverflow.com/questions/61570965/how-can-i-create-a-pdf-view-in-android