【问题标题】:Download pdf in Webview didnt work ( Error Can only download HTTP/HTTPS Uri: Blob )在 Webview 中下载 pdf 不起作用(错误只能下载 HTTP/HTTPS Uri: Blob)
【发布时间】:2021-07-10 01:51:55
【问题描述】:

我为我的网站制作了带有 webview 的 android 应用程序,但是当我尝试下载 pdf 文件时,我总是收到错误 java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: blob:http://lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2 我正在使用下载监听器,这是我的代码:

@SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.os_view);
    assert webView != null;
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowFileAccess(true);

    if(Build.VERSION.SDK_INT >= 21){
        webSettings.setMixedContentMode(0);
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }else {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    webView.setWebViewClient(new Callback());
    webView.loadUrl(webview_url);
    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.setMimeType(mimetype);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            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();
        }
    });

这是错误日志:

2021-04-15 09:13:16.790 6870-6870/mgks.os.fileup E/AndroidRuntime: FATAL EXCEPTION: main
Process: mgks.os.fileup, PID: 6870
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: blob:http://lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2
    at android.app.DownloadManager$Request.<init>(DownloadManager.java:468)
    at mgks.os.fileup.MainActivity$1.onDownloadStart(MainActivity.java:149)
    at Cs.handleMessage(chromium-TrichromeWebViewGoogle.apk-stable-410410681:156)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

谁能帮帮我???

【问题讨论】:

  • String url 开头有blob: 吗?如果是这样,摆脱它?
  • 不,当我使用浏览器时,url 不包含 blob,当我单击下载按钮时,它会自动下载文件,但是为什么在我的 android 应用程序(webview)中这些错误说“只能下载 HTTP/HTTPS URIs:blob:lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2"我搞糊涂了??

标签: java android pdf android-webview android-download-manager


【解决方案1】:

除了使用 webview 之外,您还可以使用操作视图意图直接跳转到浏览器并在其中传递 url,或者您可以尝试 volley 库

【讨论】:

  • 但是我的项目是将网站转换为android应用程序,例如facebook应用程序,所以如果我的应用程序重定向到浏览器也没用,我卡在这个下载功能中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 2019-04-02
相关资源
最近更新 更多