Loading of base64 images in WebView - android

I've got a question related to the showing HTML data inside WebView.
I have quite big HTML string with some images with src in base64 format. I'm reading this data from file. File reading is quite fast and I have no issues with this.
When it's only one image inside HTML - it works fine. But when at least a couple - it's lagging a lot. (It's loading about 30-35 seconds)
I tried to speed up it but no result.
This is the way I load HTML string.
WebView.loadDataWithBaseURL("file:///", StringEscapeUtils.unescapeHtml4(hmtl), "text/html", "UTF-8", null);
I use this function with base URL "file:///" because I'm going through the links(local) inside this file. So I guess it's obligatory to use this kind of function.
Can anyone help me with a solution?

Related

How can I embed the new GIFV format on my android app?

I would like to know if one of your could embed GIFV format into your app. I am trying to embed it using a webview but without success.
Actually GIFV format is closer to a video format than a gif.
Whatever suggestion would be nice. Thanks in advance.
After work around of this issue, I couldn't find a solution which could display GIFV format like a video.
But, my solution to this issue has been to use a webview using the same way that i was using to make able to load "gif" files and if you remove directly the "v" from the name of the file, you can show it as a .gif file.
Resuming, remove the "v" from the .gifv file name and display it as a .gif.
The way that i am injecting code to make able to display gifs into the webview is:
public void getContentWebView(String url, WebView webView)
{
String html = "<!DOCTYPE html><html><body><img src=\""+ url +"\" width=\"100%\" height=\"100%\"></body></html>";
webView.loadData(html, "text/html", "utf-8");
}

images in not loading in webview Android

I have a issue in loading images in the webview. the normal images in the src tags are loading perfectly .but there is an custum image tag.
<img class=\"lazy lazy-hidden aligncenter size-full wp-image-40307\" src=\"data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=\" data-lazy-type=\"image\" data-lazy-src=\"http://www.hdwallpapersimages.com/wp-content/uploads/2014/01/Winter-Tiger-Wild-Cat-Images.jpg\" alt=\"yoyo\" width=\"600\" height=\"400\" title=\"Title\" />
i am loding the data in the webview like below
news_deatilsWv
.loadDataWithBaseURL(null, dataString, "text/html", "UTF-8", null)
dataString contain the the entire html data which i displayed, only this custom image portion is not loading.i search the web and found it is related t the base 64 encoding but cant find how to fix this. can any one suggest an idea or solution to fix this problem.

Images not displaying in Android WebView

I have a situation where I am trying to display user-generated HTML in a webview using loadData. My code is something like this:
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<html><head>" +
"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />" +
"</head><body>";
content += htmlStr + "</body></html>";
wv.loadData(content, "text/html", "UTF-8");
It works pretty well in most cases, but I am having trouble getting certain types of images to display. In most of my testing, inserting <img src="..."/> tags worked fine, but I found that links to images on Photobucket would not display at all; I get a little box with question mark in it instead of the image.
Clicking on a link to an image on Photobucket tends to take you to the page on which that image is viewable on their website, rather than just the raw image itself. I have a feeling that the issue is related to this. I suspect that it may be a "Referer" issue, or perhaps user-agent, or something of that nature, but I cannot for the life of me get this to display properly.
I have tried switching to loadDataWithBaseURL and providing a BaseURL (as I believe this will be used as the referer url) but that made no difference. I have also tried using loadUrl("http://photobucket..." instead and providing a HashMap with the Referer header manually set, but that did not work either. Actually, switching to loadUrl made it immediately redirect to the device's browser to load the Photobucket page. I attempted to provide a custom WebViewClient and override shouldOverrideUrlLoading, but the best I got it to do was to display the full Photobucket page inside the WebView.
I am sure this is not a specific issue with Photobucket, that just happens to be the site that I discovered this problem with while I was testing.
I would really like to figure some way to deal with this situation so that this can work correctly, but I have been as yet unable to find any helpful direction on SO or the internet at large. Does anyone have any ideas?
I would wait until Photobucket finishes their recent updates before further testing...while you stated you are sure it is not a specific issue with Photobucket the internet is now awash with posters not able to view Photobucket linked to images with android based devices. If your code works with all other images not hosted on photobucket your code is good.

Android WebView: loading a local HTML file ignoring its body

I would like to know if it is possible to load a local HTML file into a WebView loading everything but the body innerHTML. That is, the resulting DOM will have head, scripts and CSS's but the body will be empty. I tried emptying the body just after loadUrl call but the WebViews goes on loading the body. I want the body is not loaded at all so to speed up loading, that is, no rendering has to be done by the WebView.
It's a bit of a hack by why not try this:
String html = loadHtmlFromFile();
String newHtml = html.replaceFirst("<body>.*<\body>", "<body><\body>");
This will replace the body with just the body tags.
Then either save newHtml as an html file and open that or, if possible, pass the HTML directly to the WebView (although I'm not sure you can do that)
You'll also have to write the loadHtmlFromFile() method to get your HTML for you.

Why is WebView unable to open some local URLs (Android)?

I have a WebView that I'm using to open some files stored in the assets/ directory of my project. It works fine for most of the files, but there's one in particular (and I'm sure others I haven't found) that it just will not open.
The file I'm having problems with is named:
"assets/ContentRoot/Photos/XXX Software Logo - jpg - 75%.JPG"
When I pass it to WebView, and it shows the error page, it shows it as:
"file:///android_asset/ContentRoot/Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG"
I then tried running URLEncoder.encode() on it and got the error page with the URL presented as:
"file:///android_asset/ContentRoot/Photos/XXX+Software+Logo+-+jpg+-+75%.JPG"
Neither of these URLs were able to open the file (and they both look okay to me). Anyone have any ideas?
UPDATE: If I encode the % by hand (using %25, as commonsware.com suggested) then it loads the image, but it tries to parse it as text, not as an image, so I just get a lot of (basically) garbage.
Also, referring to the image in an HTML document with a relative URL isn't working (probably because it's not being parsed as an image?):
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG" />
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%25.JPG" />
Okay, after spending way too long on this, I've figured out what's going on. Basically, if images stored in the assets/ directory contain a space (e.g., " ") in their file name, they won't render as images.
myWebView.loadUrl("file:///android_asset/testimage.jpg");
works fine. However,
myWebView.loadUrl("file:///android_asset/test+image.jpg");
just throws a not found error and
myWebView.loadUrl("file:///android_asset/test image.jpg");
// and
myWebView.loadUrl("file:///android_asset/test%20image.jpg");
show it improperly displayed (as text... see screenshot in question).
This unexpected behaviour is present on (at least) 1.5, 1.6, and 2.0 and I filed a bug report.
Try getting rid of the % in the filename. Or, escape it as %25.
I would guess that WebView only understands text related content types so it faithfully treating your JPG as base64 encoding, decodes and displays resulted gobble-goop as text. I don't really know if it's possible to set content type for WebView but as workaround you can try to throw img tag inside html tag and load resultet page. Also you probably can only use WebView#loadDataWithBaseUrl

Categories

Resources