WebView + Blob URL + Download Image

Vishal Patel picture Vishal Patel · Jul 18, 2017 · Viewed 8.7k times · Source

After a Long Time, I was Used WebView. so let me ask what I have a phase issue.

-I had Google for Blob URL but I hvnt find a solution OR Any hint so I have to post Question HERE.

-I have WebView at the android side to Load A website. At their On Button to "SAVE-AS" to download an image.

-Now when I trying it on Google Chrome app yeah its working fine and download properly.

-Now the same scenario with webview at my app at DownloadListener I got Blob URL something like

blob:http://-----cantshare-----.com/7e7452c8-62ca-4231-8640-0e9de715e073

So downloadmanger download nothing

Check my code

webview = (WebView) findViewById(R.id.webview);


WebSettings settings = webview.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

final ProgressDialog progressBar = ProgressDialog.show(this, "WebView Example", "Loading...");

webview.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "Processing webview url click...");
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, String url) {
        Log.i(TAG, "Finished loading URL: " + url);
        if (progressBar.isShowing()) {
            progressBar.dismiss();
        }
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.e(TAG, "Error: " + description);

    }
});

webview.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

        Uri webpage = Uri.parse(url);

        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            webpage = Uri.parse("http://" + url);
        }

        DownloadManager.Request request = new DownloadManager.Request(
                webpage);


        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.setDestinationInExternalFilesDir(Main2Activity.this,
                Environment.DIRECTORY_DOWNLOADS,".pdf");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File",
                Toast.LENGTH_LONG).show();
    }
});

Q1 -Can Anyone give suggestion How to download File From Blob URL ?
Q2 - Have to change At Server side ?
Q3 - any other ways instead of Downloadermanager?

Edit

I tried .pdf /.png/.jpeg but no luck ;(

setDestinationInExternalFilesDir(Main2Activity.this,
                Environment.DIRECTORY_DOWNLOADS,".pdf");

Answer

Kevin Perez picture Kevin Perez · Feb 23, 2018

I was in the same nightmare. What I did is convert the Blob URL data in Base64 String and with this string I created a ".pdf" file. Take a look of my answer.