【问题标题】:Java: How to automatically open pdf after DownloadJava:下载后如何自动打开pdf
【发布时间】:2019-01-01 02:53:25
【问题描述】:

我一直在寻找各种教程,但没有人能解决我的问题。我希望这里可以让我理解,关于如何在完成从数据库下载到 Android 后自动打开 PDF 文件。

这是我的下载脚本。

@Override
public void onBindViewHolder(HolderData holder, int position) {
    final ModelData md = mItems.get(position);
    holder.txtname.setText(md.getName());
    holder.txtwaktu.setText(md.getWaktu());

    //Proses Downloading
    holder.relativeLayoutMateri.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            uri = String.valueOf(Uri.parse("http://192.168.43.144/MLearning/crud/"+md.getPath()));
            dm = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            longid = dm.enqueue(request);

            Toast.makeText(context, md.getName()+" Berhasil Di Download"+md.getPath(), Toast.LENGTH_SHORT).show();
        }
    });
}

【问题讨论】:

  • "saya baru belajartentang android, jadi saya harap penjelasan yang bisa membuat saya mengerti.sebelumnya terimakasih" SO的这个领域是用英语进行的,所以请使用最好的英语你可以管理。评论被删除,因为它无论如何都是无关紧要的。 (无需提及您是“X 新手”。)
  • 我认为这可以帮助你stackoverflow.com/questions/5877753/…

标签: java android pdf download


【解决方案1】:

这里是解决方案,请使用ACTION_VIEW Intent打开所有你需要的下载pdf的路径。

 private static String filepath = Environment.getExternalStorageDirectory().getPath()+"/myfile.pdf";   

    File file = new File(filepath);

    if (file.exists()) {
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(intent);
        } 
        catch (ActivityNotFoundException e) {
            Log.d(TAG,e.getMessage());
        }
    }

【讨论】:

  • 当您将文件保存在内存中时,它将返回一个文件路径,只需将该路径添加到文件路径变量中。
  • 对不起,path变量来自数据库,也就是material/file.pdf 会调用什么地址?或下载后文件的位置。 maap 太多的贝塔尼亚。在我使用上面的代码之前,它会自动打开,但数据没有出现。 #通过谷歌翻译。对不起
猜你喜欢
  • 2021-10-30
  • 2014-06-06
  • 2015-04-20
  • 2014-06-22
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
相关资源
最近更新 更多