I have an Android Webview where I load HTML into using loadDataWithBaseURL, some of my images are displayed a "?" as if the image is missing. I had trouble fixing that so I'm now attempting to remove images altogether. I tried to use:
webview.loadUrl("javascript:(function() { " + "document.getElementsByTagName('img')[0].style.display = 'none'; " + "})()");
But it didn't work, I'm struggling to think of another solution. The HTML code producing the images is formatted like so:
<img name="StartDate" src="images/calendar.gif" onclick="displayDatePicker('StartDate',false,'dmy','-')" alt="Press for a date picker" />
Any help would be greatly appreciated.
Try using:
webView.getSettings().setLoadsImagesAutomatically(false);
Related
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");
}
I have pictures/movies in my webview, and i have a problem with the resize.
I have a CSS for my webview in which i set
img{width:100%;}
But how to set the just height ? Because currently I have picture which take the half of the screen.
My other problem is i have
in my content, how can I delete it ?
To modify the styling of the HTML you could load a custom css like this:
final String customCssLink = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">";
webView.loadDataWithBaseURL("file:///android_asset/", customCssLink + html, "text/html", "UTF-8", null);
And you can remove all of the from the HTML like this:
String.replace(" ", "");
But with both those things you have to be very careful. Modifying HTML is just like HTML parsing a big source of bugs and rather error prone. If at all possible I would advice against doing stuff like this, but if you have no other choice try to at least be as careful as possible.
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.
I tried many times and try to use HTML tag <object>, but app just crash and disappear.
Each time when I use normal html tag such as <html>, <body>, <h1>, <div>, <td>, <tr> etc. Those can be shown directly without any error.
This time I try to embed an <object> tag to webview and it always crash!
Does anyone who know how to do this directly inside code? OR, webview just cannot do this?
here is my code.
String html =
"<html>" +
"<body><h1>This is a test !!</h1>" +
"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='200' height='200'></object>" +
"<param name='quality' value='high'>" +
"<param name='allownetworking' value='internal'>" +
"<param name='allowScriptAccess' value='never'>" +
"<param name='movie' value='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1'>" +
"<param name='allowFullScreen' value='true'>" +
"<embed src='http://video.yutube.com/flv2.swf?i=20100129LqvamMPB&d=360.25&movie_stop=off&no_progressive=1&otag=1&sj=5&rel=1' width='200' height='200' quality='high' allownetworking='internal' allowscriptaccess='never' allowfullscreen='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed>" +
"</object>" +
"</body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
You have one <object> tag and two </object> tags. The first </object> has got to go.
The embed tag is for browsers that don't recognize the object tag. Those browsers will see the embed tag and will try to use it. Browsers that support the object tag will use the object and ignore the embed, as long as the embed is within the object. If the embed is outside the object, object aware browsers may load both the object and embed tags.
WebView can definitely handle the html tag. I don't think you should use loadDataWithBaseURL though, try this instead:
mWebView.loadData(html, "text/html", "UTF-8");
I'm not too sure your <object> will work though...
Here's a better way to play a YouTube video if that's all you need:
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=<video-id>")));
Put your video id in place of the <video-id>
I have a problem with an URL that starts with https. It's showing a blank white screen whereas links, starting with http, loads successfully in Android webview.
With logs I'm sure that data is coming from HTTPS also but is not displaying in the view. Please help me. Some sample code or link will be most appreciated.
HI,
I read your question, I don't know what is your exact issue if you have data then you can use these line of code to display it in webview
encoding contains html tags like <p> aadfasdf asdf ds </p>
public static final String HTML_FORMAT =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">" +
"<html><head><title></title>"+
"<style type=\"text/css\">" +
"'body {color:#FFFFFF;background-color:#000000;font-size: 10pt}'" +
"</style>" +
"</head>" +
"<body>" +
"%s" +
"</body></html>";
String textHtml = String.format(Constants.HTML_FORMAT,encoding);
wv.setBackgroundColor(Color.BLACK);
wv.loadDataWithBaseURL(null, textHtml, "text/html","utf-8", null);
try this hopefully you will get your required result.