问题
I have fetched a PDF list view from Firebase Database
This is the code that i used to fetch
public class ViewFiles extends AppCompatActivity {
ListView myViewFiles;
DatabaseReference databaseReference;
List<uploadFiles> uploadDOCS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_files);
myViewFiles = (ListView) findViewById(R.id.myViewFiles);
uploadDOCS = new ArrayList<uploadFiles>();
viewAllFiles();
myViewFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
uploadFiles uploadFiles = uploadDOCS.get(position);
Intent intent = new Intent();
intent.setType(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uploadFiles.getUrl()));
intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
startActivity(intent);
}
});
}
private void viewAllFiles() {
databaseReference = FirebaseDatabase.getInstance().getReference("HNDIT").child("1st Year 2nd Sem").child("ENGLISH");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()){
uploadFiles uploadFiles = postSnapshot.getValue(com.example.lms.fileupload.uploadFiles.class);
uploadDOCS.add(uploadFiles);
}
String[] uploads = new String[uploadDOCS.size()];
for (int i=0; i < uploads.length; i++){
uploads[i] = uploadDOCS.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,uploads){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView myText = (TextView) view.findViewById(android.R.id.text1);
myText.setTextColor(Color.BLACK);
return view;
}
};
myViewFiles.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
This is how it displays Click here to View
And i have create a intent for view these file in PDF view in android but it won't display keep blank this is the error it shows
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)
this is the code of that
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();
}
}
}
This view parts xml code The Xml file
how can i fix this error and view files in android...
This is how the files in my firebase database the database view
回答1:
I want to start my answer with what you did wrong here :
Intent intent = new Intent();
intent.setType(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uploadFiles.getUrl()));
intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
startActivity(intent);
You did all the important stuff in your intent
object like setting it's type
and data
and then finally you ditched that instance for a new Intent
instance. So now your intent
object does know where to go i.e. ViewPdfFiles.class
but does not have data
or type
set to it. Changing first line of code toIntent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
and removing 4th line should work.
Edited answer :
Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);;
//send data using putExtra(String, String) method if uploadFiles.getUrl() is string
intent.putExtra("url", uploadFiles.getUrl());
startActivity(intent);
the null object
来源:https://stackoverflow.com/questions/61588920/how-to-view-pdf-from-a-list-view-which-is-fetch-from-firebase-database