Java:下载后如何自动打开pdf

时间:2018-07-24 13:04:23

标签: java android pdf download

我一直在寻找各种教程,但没人能解决我的问题。我希望这里能使我理解,关于从数据库下载到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();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

这是解决方案,请使用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());
        }
    }