10

I have a server that creates an object blob on the browser and I want to download this within WebView in an Android app. I tried redirecting the request to the browser instance as well as using the download manager to do this, but neither of them seems to work (Even though if I open up the same page in Chrome, the download operation works there).

I tried the following code,it throws error:

Android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:https%3A//111.111.111.111%3A8080/40b63131-63b1-4fa4-9451-c6297bbd111a"

Edit

Android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=blob:http://digitalinsensu.com/0f0d6127-a0f1-44c3-af85-72f648258d6d


Code:

mWebView.setDownloadListener(new DownloadListener() {
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);
}
});

and this throws java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs error:

mWebView.setDownloadListener(new DownloadListener() {
    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);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);

    }
});

How should I be downloading the blob? Any help would be appreciated.

12
  • What are you expecting to happen instead of the crash? Commented Feb 15, 2016 at 2:35
  • Why would you use a webview to download an object? It won't know how to parse anything but a webpage. Use DownloadManager instead. Commented Feb 15, 2016 at 2:39
  • 1
    @GabeSechan I load a url in my webview that does a bunch of things and creates a blob, and I want to download that to the device. Commented Feb 15, 2016 at 2:43
  • 1
    @DougStevenson I understand that the code does not work because it tries to operate on a blob saved on the browser rather than an HTTP address, but I have not been able to find how to download a blob in Android. Commented Feb 15, 2016 at 2:46
  • @GulizTuncay If the url string is just "blob:" I don't think there's anything you can do with that to get to the data you want. Commented Feb 15, 2016 at 2:55

1 Answer 1

0

You need to intercept the callback for handling url clicks like this:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        shouldOverrideUrlLoading(view, Uri.parse(url));
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return shouldOverrideUrlLoading(view, request.getUrl());
    }

    // legacy callback
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return shouldOverrideUrlLoading(view, Uri.parse(url));
        }
        return super.shouldOverrideUrlLoading(view, url);
    }

    private boolean shouldOverrideUrlLoading(final WebView view, final Uri request) {
        if(request.getScheme().equals("blob")) {
            // do your special handling for blob urls here
            return true;
        }
    }
});

In shouldOverrideUrlLoading() you can do your special handling.

Sign up to request clarification or add additional context in comments.

7 Comments

Which type of special handling please specify? bdw shouldOverrideUrlLoading() is not called at all :-(
What do you want to do with that url?
try this special handling: webView.loadUrl(request.toString().substring(5));
How should the correct http address look like? I mean there is no official blob protocol.
shouldOverrideUrlLoading() isn't triggered at all for this download (tested with Webview 66)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.