Android Question WV_OverrideURL event not being triggered for a Data:Base64 link

JohnC

Well-Known Member
Licensed User
My app needs to display an image from a webpage in which the link on the webpage is the actual data of the image.

In other words, when I hover over the link on the webpage, and select "Copy Link Address" the clipboard then contains this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA.....................................................Jggg==

(where the "........" is actually a bunch of base64 encoded data of the image that I removed from the above line so it wont take up the entire screen.

If I click this link in a desktop Firefox browser, it will prompt me to "Open" the file(data) with an image viewer or to save the file to disk.

In order to display the image from within my app, I first need to download the data in that link.

But, when I click the link in a webview, the wv_overrideurl event never triggers - webview just sits there with a dumb look on its face.

Anyone have an idea why the event doesn't trigger for this special type of "data:" link?
 

Devv

Active Member
Licensed User
first: if i remembered right there is a limit url length
second: there is lots of easier and cleaner way to download an image why you want to do this ?
 

JohnC

Well-Known Member
Licensed User
I don't have much control over the webpage that has the link setup that way, so I was hoping to be able to find a way to download the image as-is without having to deal with getting the creator of the webpage to make any changes on their end.
 

Erel

Administrator
Staff member
Licensed User
You can download the html page with OkHttpUtils2 and find this data in the downloaded page.
 

JohnC

Well-Known Member
Licensed User
But then how can I detect when the user wants to see the image and clicks on the link?
 

JohnC

Well-Known Member
Licensed User
The webpage is behind a paid firewall, so I cant get the source for it :(

It looks like Webview is not designed to detect clicks on links that start with "data:"
 

DonManfred

Expert
Licensed User
It looks like Webview is not designed to detect clicks on links that start with "data:"
Webview is designed to detect clicks on "a" Tags.

Firefox did detect the click on an img Tag which is not filled by a src url but by src-data.
If is a side-feature of Firefox.
 

Erel

Administrator
Staff member
Licensed User
I've tried to add a download listener to WebView, however the event is not raised for data uris. You can try it with your page:
B4X:
Sub Globals

   Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim dl As JavaObject
   dl.InitializeNewInstance(Application.PackageName & ".main$MyDownloadListener", Null)
   dl.RunMethod("set", Array(WebView1))
   WebView1.LoadUrl("https://flaviocopes.com/data-urls/")
End Sub

Sub DownloadListener_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Return Null
End Sub

Sub WebView1_OverrideUrl (Url As String) As Boolean
   Log(Url)
   Return False
End Sub

Sub WebView1_PageFinished (Url As String)
   Log("page: " & Url)
End Sub

#if Java
public static class MyDownloadListener implements android.webkit.DownloadListener {
public void set(android.webkit.WebView wv) {
   wv.setDownloadListener(this);
}
 public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimeType,
                                long contentLength)
       {
       BA.Log("Test");
       }
       }
#End If
If you see Test in the logs then the link click was detected.
 

JohnC

Well-Known Member
Licensed User
Webview is designed to detect clicks on "a" Tags.

Firefox did detect the click on an img Tag which is not filled by a src url but by src-data.
If is a side-feature of Firefox.
Thanks for the info on the "a" tag - I did not know that.
 

JohnC

Well-Known Member
Licensed User
OK, I got the "Test" in the log.

So I modified the Java (I am just starting to understand Java) to this (based on your java code in this thread: https://www.b4x.com/android/forum/threads/upload-files-with-webview.98623/):

B4X:
public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long contentLength)
       {
       processBA.raiseEventFromUI(this, "ondownloadstart", url, userAgent, contentDisposition, mimetype, contentLength);
       }
       }
And added this event:

B4X:
Sub onDownloadStart (url As String, userAgent As String, contentDisposition As String, mimeType As String, contentLength As Long)
    Log("url: " & url)
    Log("userAgent: " & userAgent)
    Log("contentDisposition: " & contentDisposition)
    Log("mimeType: " & mimeType)
    Log("contentLength: " & contentLength)
End Sub
Only the url returns a value (below) - the other vars are blank and contentLength returns -1:

data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgM...................MDAsKCwsNDhIQDQ4RDg

So now I can work with that to decode the data and display the image.

Thanks Erel!
 
Last edited:
Top