[我已经从Firebase数据库中获取了PDF列表视图
这是我以前获取的代码
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) {
}
});
}
}
这是它的显示方式Click here to View
而且我已经创建了一个在Android的PDF视图中查看这些文件的意图,但是它不会显示为空白,这是它显示的错误
E / PDFView:加载pdf错误 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int java.io.InputStream.read(byte [])' 在com.github.barteksc.pdfviewer.util.Util.toByteArray(Util.java:36) 在com.github.barteksc.pdfviewer.source.InputStreamSource.createDocument(InputStreamSource.java:37) 在com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53) 在com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25) 在android.os.AsyncTask $ 2.call(AsyncTask.java:333) 在java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641) 在java.lang.Thread.run(Thread.java:764)
这是该代码
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();
}
}
}
此视图部分xml代码 The Xml file
我该如何解决此错误并在android中查看文件...
这是我的Firebase数据库中的文件的方式the database view
我想从你在这里做错的地方开始回答:
Intent intent = new Intent();
intent.setType(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uploadFiles.getUrl()));
intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
startActivity(intent);
您在intent
对象中完成了所有重要的操作,例如将其设置为type
和data
,然后最终您放弃了instance来创建new Intent
实例。因此,现在您的intent
对象知道要去哪里,即ViewPdfFiles.class
,但没有为其设置data
或type
。将第一行代码更改为Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
并删除第四行应该可以。编辑后的答案:
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);
空对象