如何通过Android Web视图下载PDF文件?

发布于 2025-02-04 08:36:36 字数 1530 浏览 2 评论 0原文

我有一个Android Web视图,可以很好地工作。 我添加了以下代码以下载PDF文件。

webview.setDownloadListener(new DownloadListener() {

            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                        Toast.LENGTH_LONG).show();

            }
        });

尽管它在网络上工作得很好,

但是在Android WebView App中:

  • 它给了我“下载文件”的应用程序内通知,
  • 显示了一段时间的加载程序
  • 从下载管理器中说“下载失败”
  • ,然后在通知栏上发出通知,请 。请注意,我也在清单中也允许。

有关 mainActivity.java 的完整代码。

任何帮助将不胜感激。

I have an android web view which works perfectly fine.
I added the below code to download the PDF file.

webview.setDownloadListener(new DownloadListener() {

            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                        Toast.LENGTH_LONG).show();

            }
        });

Although it works perfectly fine on the web,

But in Android Webview App :

  • It gives me an in-app notification of "Downloading File"
  • Shows the loader for a while
  • Then a notification on the notification bar says "Download Unsuccessful" from Download Manager
  • Please note that I have also given permission in Manifest as well.

For the full code of MainActivity.java please refer to the link.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2025-02-11 08:36:37

这个来自YouTube的家伙为我解决了它。

public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
   }
});

但是问题是该应用程序正在重定向到Chrome以加载URL并让我下载文件。还有其他方法可以在后台加载URL并下载文件吗?

希望它能帮助某人。

This guy from youtube solved it for me.

public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
   }
});

But the issue is that the app is redirecting to chrome to load the URL and letting me download the file. Is there any other way to load the URL in the background and download the file?

Hope it helps someone..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文