【问题标题】:Open a Downloaded file (pdf) from the Downloads folder in the INTERNAL STORAGE从内部存储的下载文件夹中打开下载的文件 (pdf)
【发布时间】:2016-11-01 11:07:24
【问题描述】:

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

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

我正在使用以下代码尝试从下载文件夹中检索 pdf,但我在 Toast 上收到错误消息,提示“无法显示 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);

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

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: android file pdf android-intent android-download-manager


    【解决方案1】:

    如果您正在为 Lollopop 及以下公司工作:您不必在运行时向用户询问权限。清单权限就可以了。

    如果您正在为 Marshmellow 及更高版本工作:您必须在运行时向用户询问 permission 并根据用户输出进行操作。

    记住:您仍然必须在清单上授予权限。

    在用户下载文件夹中下载 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);
    

    Reference

    从用户设备的下载文件夹中查看下载的 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 文件之前,请确保您获得了许可。

    【讨论】:

    • 下载失败..知道为什么会这样吗?
    • 它工作正常但是。有一个关于“Environment.getExterbalStoragePublicDirectory”的问题给出了已弃用的文件。请更新新代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    相关资源
    最近更新 更多