How to read the data in a Blob in WebView on Android?

16,331

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

Share:
16,331
Alice Van Der Land
Author by

Alice Van Der Land

Updated on June 27, 2022

Comments

  • Alice Van Der Land
    Alice Van Der Land almost 2 years

    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.

    • Doug Stevenson
      Doug Stevenson about 8 years
      What are you expecting to happen instead of the crash?
    • Gabe Sechan
      Gabe Sechan about 8 years
      Why would you use a webview to download an object? It won't know how to parse anything but a webpage. Use DownloadManager instead.
    • Alice Van Der Land
      Alice Van Der Land about 8 years
      @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.
    • Alice Van Der Land
      Alice Van Der Land about 8 years
      @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.
    • Doug Stevenson
      Doug Stevenson about 8 years
      @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.
    • Gabe Sechan
      Gabe Sechan about 8 years
      Definitely seems like your url isn't right. It needs to be a fully qualified url
    • Alice Van Der Land
      Alice Van Der Land about 8 years
      @DougStevenson I just did not copy paste the whole url in there.
    • Alice Van Der Land
      Alice Van Der Land about 8 years
      @GabeSechan Yes, seems like those methods only work with proper URLs and not blobs. I couldn't figure out how to access the blob from java though.
    • Gabe Sechan
      Gabe Sechan about 8 years
      There's got to be a url starting with http or https to access the data. blob: is not a protocol to transfer data with.
    • Doug Stevenson
      Doug Stevenson about 8 years
      It looks like there is an encoded url in the blob url. Is that what you're trying to download?
    • Alice Van Der Land
      Alice Van Der Land about 8 years
      I'm trying to download the audio data the blob contains.
    • Maveňツ
      Maveňツ over 7 years
      @DougStevenson We are expecting the same behavior as Android Native Browser.i.e File Downloads perfectly in browser.
  • Maveňツ
    Maveňツ over 7 years
    Which type of special handling please specify? bdw shouldOverrideUrlLoading() is not called at all :-(
  • rekire
    rekire over 7 years
    What do you want to do with that url?
  • rekire
    rekire over 7 years
    try this special handling: webView.loadUrl(request.toString().substring(5));
  • rekire
    rekire over 7 years
    How should the correct http address look like? I mean there is no official blob protocol.
  • loretoparisi
    loretoparisi about 6 years
    I have tried a similar approach, but with no success on a macOs WebView, but having the same problem here - stackoverflow.com/questions/49988202/…
  • Alexey Ozerov
    Alexey Ozerov almost 6 years
    shouldOverrideUrlLoading() isn't triggered at all for this download (tested with Webview 66)
  • rekire
    rekire almost 6 years
    @AlexeyOzerov did you override all overloads like in my example code? That is working fine for me.
  • Alexey Ozerov
    Alexey Ozerov almost 6 years
    Yes, none of the overridden shouldOverrideUrlLoading() methods is triggered with SDK 23 and SDK 24 Webview 66 using this test jsfiddle.net/3khffu41