'Download pdf in Webview didnt work ( Error Can only download HTTP/HTTPS Uri: Blob )

i make android app with webview for my website, but when i try to download pdf file i always get error java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: blob:http://lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2 im using downloadlistener, this is my code :

@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();
        }
    });

and this is the error log :

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)

can anybody help me???



Solution 1:[1]

apart from using the webview you can use the action view intent to directly jump on to the browser and just pass the url inside it or else you can try the volley library

Solution 2:[2]

I had the similar issue the onDownloadStart param url was appended with blob:. I Just removed it from the url and it worked like a charm.

url that was coming from method -> blob:http://lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2

url that I had to use -> http://lapor.infokanimsambas.com/a0d3f9a5-81ce-434d-99a3-52ae66348ea2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lalit sharma
Solution 2 Rahul ch