【问题标题】:how to view PDF from a list view which is fetch from Firebase database?如何从从 Firebase 数据库获取的列表视图中查看 PDF?
【发布时间】:2020-08-18 16:25:00
【问题描述】:

我从 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

【问题讨论】:

  • 我认为您的 inputStream 返回 null。调试并检查你得到的数据。
  • 我已经调试和检查过了。它同时获取两个文件,但解析最后上传到数据库的 1files url。我很困惑我应该在哪里更正。
  • 你能用你从服务器得到的响应更新你的问题吗?
  • 使用调试选项编辑
  • 嗨,你能告诉我你在 RetrievePDFStream 后执行方法中得到了什么。输入流的数据。

标签: java android firebase-realtime-database android-activity pdfview


【解决方案1】:

我想从你在这里做错了什么开始我的回答:

        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强>实例。所以现在你的intent 对象确实知道去哪里,即ViewPdfFiles.class,但没有设置datatype。将第一行代码更改为
Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
并删除 第 4 行 应该可以工作。
已编辑答案:

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

空对象

【讨论】:

  • 它没有得到任何东西,只是白屏。有什么问题。我会用我看到的图像编辑你的文字
  • 对不起,我没有看到你的ViewPdfFiles.java。为了能够像在那个活动中那样检索String,您需要使用putExtra() 方法而不是使用setData() 发送数据。请参阅我编辑的答案。
  • 它没有工作给一个空对象我将它添加到你的代码中。 (我从我的 firebase 数据库中获取文件,我会将其添加到相同的代码中)
  • 查看我的最新编辑。我在上次编辑中犯了同样的错误,我建议你不要在我未经编辑的答案 xD 中这样做。
  • 还是同一个兄弟,我的 ViewPdfFile.class 代码是否正确?我编辑了在 RUN 部分给出的错误
猜你喜欢
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 2019-10-01
相关资源
最近更新 更多