从外部应用下载并打开PDF文件

时间:2018-12-12 15:38:14

标签: android file pdf

您好,我很想打开下载的pdf文件。我可以下载它,但无法使用外部应用程序将其打开。

如下面的代码所示:

    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimeType,
                                    long contentLength) {
            Intent intent = getIntent();

            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                    mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);

            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();



            Uri uri = Uri.parse(Environment.DIRECTORY_DOWNLOADS + URLUtil.guessFileName(
                    url, contentDisposition, mimeType));
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(uri, "application/pdf");
            target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            getApplicationContext().startActivity(target);
        }
    });

问题是我找不到此行生成的文件名:

request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));

我尝试过:

Uri uri = Uri.parse(Environment.DIRECTORY_DOWNLOADS.toString() +"/"+ URLUtil.guessFileName(
                    url, contentDisposition, mimeType));

0 个答案:

没有答案