从“内部存储”中的“下载”文件夹中打开已下载的文件(pdf)



我使用下载管理器下载了一个文件(133465.pdf),现在它存储在手机的下载文件夹(内部存储)

我应该如何尝试和检索下载的pdf从下载文件夹?

我正在使用下面的代码尝试从下载文件夹检索pdf,但我在吐司上得到一个错误,说"无法显示pdf (133465.pdf无法打开)".

String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() +  File.separator + "133465.pdf";
                Log.i("Fragmentadapter1", file);
                File videoFile2Play = new File(file);
                Intent i = new Intent();
                i.setAction(android.content.Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(videoFile2Play), "application/pdf");
                imageContext.startActivity(i);

我不知道我是否使用了正确的文件位置来访问文件。

如果您正在为Lollopop及以下版本工作:您不必在运行时请求用户许可。manifest Permissions就可以了

如果您正在为Marshmellow及以上工作:您必须在运行时请求用户许可,并根据用户的输出进行操作。

记住:你仍然需要在Manifest上给予权限。

下载用户的PDF文件下载文件夹:

DownloadManager downloadmanager;
    Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .mkdirs();
    downloadmanager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
    String url = hardcode + bb ;
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri)
            .setTitle(bb )
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    bb)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    Log.i("Download1", String.valueOf(request));
    downloadmanager.enqueue(request);
参考

从Users设备的Downloads文件夹中查看已下载的PDF:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
            "YOUR FILE NAME");
    Uri path = Uri.fromFile(file);
    Log.i("Fragment2", String.valueOf(path));
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        this.startActivity(pdfOpenintent);
    } catch (ActivityNotFoundException e) {
    }

注意:在下载查看Marshmellow和UP的PDF文件之前,请确保获得许可。

最新更新