Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 312
Alice Van Der Land Avatar asked Feb 15 '16 02:02

Alice Van Der Land


1 Answers

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

like image 117
Maveňツ Avatar answered Sep 16 '22 14:09

Maveňツ