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.
Related
I insert the code of the page in WebView, but its code is cut off.
Both elements are at the bottom
String html = readFile("index.html"); webWiev.loadData(html, "text/plane; charset=utf-8", "utf-8"); T.setText(html); //EditText T = findViewById(R.id.editTextTextMultiLine);
Both elements get information from the same html variable
The page was broken so I rendered it as code, that's how I discovered the problem. ("text/plane; )
The code contains all the necessary script style pages, a total of 3496 strips, and 96,501 characters.
What could be the problem? I did not find it on the Internet. Maybe some webView limit.
The Android application must download the web application from the server or, if there is no connection, from a file. Accordingly, the page is displayed incorrectly when loaded from a file, I specify the entire web application with styles and scripts in one file. Everything I did was aimed at identifying the problem. And when I found it, I started looking for it on the Internet, but I couldn't find it.
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?
I have a web site that I stored locally into myProject/assets . I want to create a WebView to show it.
I use this code:
myWebView.loadDataWithBaseURL("file:///android_asset/", htmlContent, "text/html", "utf-8", null);
Where htmlContent is a string and it is correctly loaded from the local files.
Unfortunately, the site has absolute links: they all start with a "/" and when such a link is clicked by the user, I get this error in the WebView:
file:///linkedpage.html not found
So, as the documentation says, only relative links and not absolute links are preceded by the BaseURL. Indeed, if I remove the initial "/" from the links, it works.
This can be an error of how the site was written or not, but I'm not the writer of it and I cannot change it (and anyway it makes sense, because the html links are actually in the root of the site, so it's not wrong to call them with a "/".)
How can I manage this? Is there a way to intercept clicks on the links and modify their url, for example? Or can I effectly teach the WebView that "file:///android-asset/" is really the root of the site, and not "file:///", apache-style? Or is there another cleaner solution (besides changing the html file, that I don't want to do)
I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.
I have some images that I loaded from a remote source stored in Bitmap variables and I want to display them. In addition to switching between these images the user should also be able to zoom and pan them. My first idea was to somehow pass them via an intent to the built-in gallery application but this doesn't seem to be possible.
A solution that is suggested in several places is using a WebView since it already supports zooming and panning.
My question is how does my Bitmap data get into the WebView? Do I have to write it to a file first, which I would have to remove again later, or is there an easier way?
Or are there even better ways to accomplish my main goal, which is displaying Bitmap data as zoomable and panable images?
You can just use webview to directly view your image remotely. You do not need to save anymore the image in a file.
Here is a sample code snippet.
myWebView.getSettings().setBuiltInZoomControls(true); //to get zoom functionalities
String url = "http://....."; //url of your image
String x= "<html><head><meta name=\"viewport\" content=\"width=device-width, minimum-scale=1.0\"/><style type=\"text/css\">html, body {margin: 0;padding: 0;} img {border: none;}</style><head><body style=\"background: black;\"><table><tr><td align=\"center\"><img src=\"" + url + "\" /></td></tr></table></body></html>";
myWebView.loadData(x, "text/html", "UTF-8");
About switching images, you can just change the value of the url and call the loadData again of the webview.
I wasn't satisfied with WebView after all so I ended up creating my own image viewing Activity. Further descriptions on how I did it can be found in this post on google groups.