Skip to content Skip to sidebar Skip to footer

How To Read The Data In A Blob In Webview On Android?

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

Solution 1:

Yes, it should be possible. In your case, the URL looks like this, once the blob: prefix is removed, and the rest is URL decoded:

blob:http://digitalinsensu.com/0f0d6127-a0f1-44c3-af85-72f648258d6d

Be aware that this URL will only work if the user has a web server running on their device, which responds to requests, which is unlikely in case of blob: URLs. If you want to want to do this, you'd need to remove the blob: first, using Java's String.replace(..) or similar. To decode the URL, use something like URLDecoder.decode(url, "UTF-8");, see here for details.

The issue is that the DownloadListener is really expecting a URI (ex http://server.com/file.pdf). However the link that the user clicks on does not pass a URI in that format. The URL passed represents a blob that is attached to the DOM of the browser. As a result the solution will not work as designed.

You may have another option. It depends on how you are generating the bytes for the blob. You mentioned that you are doing that in Javascript. If so, I would skip using the Blob and URL.createObjectURL completely and write an Javascript/Native interface that will allow you transfer the bytes in chunks (n number of arrays of 255 bytes for example) to the Java code. This will keep the memory consumption down on the client.

I have an idea on how to create the interface but I first need to understand how you are generating the bytes for the blob. If you can post a byte array I may try further.

For Full Detail on this check here

Solution 2:

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

webView.setWebViewClient(newWebViewClient() {
    @OverridepublicvoidonPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        shouldOverrideUrlLoading(view, Uri.parse(url));
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        returnshouldOverrideUrlLoading(view, request.getUrl());
    }

    // legacy callback@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            returnshouldOverrideUrlLoading(view, Uri.parse(url));
        }
        returnsuper.shouldOverrideUrlLoading(view, url);
    }

    privatebooleanshouldOverrideUrlLoading(final WebView view, final Uri request) {
        if(request.getScheme().equals("blob")) {
            // do your special handling for blob urls herereturntrue;
        }
    }
});

In shouldOverrideUrlLoading() you can do your special handling.

Post a Comment for "How To Read The Data In A Blob In Webview On Android?"