如何从Firebase数据库获取的列表视图中查看PDF? [英] how to view PDF from a list view which is fetch from Firebase database?

查看:90
本文介绍了如何从Firebase数据库获取的列表视图中查看PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从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) {

        }
    });

}

}

这是它的显示方式 单击此处查看

我已经创建了一个意图,以便在android的PDF视图中查看这些文件,但是它不会显示为空白,这是它显示的错误

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:加载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)

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)

这是那个的代码

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代码 Xml文件

我该如何解决此错误并在android中查看文件...

how can i fix this error and view files in android...

这是我的Firebase数据库中文件的方式 数据库视图

推荐答案

我想从你在这里做错的地方开始回答:

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

您已经完成了intent对象中所有重要的操作,例如将其设置为typedata,然后最终您放弃了 instance 以获得了新的Intent strong>实例.因此,现在您的intent对象确实知道要去哪里,即ViewPdfFiles.class,但没有为其设置datatype.将第一行代码更改为
Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
并删除第四行应该可以.
修改后的答案:

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 to
Intent 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);

空对象

这篇关于如何从Firebase数据库获取的列表视图中查看PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆