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)
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 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 am working on a developing an Android application that displays news articles from a database through JSON. The article is in HTML format because the database is used for both web and the app. The code I have (below) works great. The format is the same on both web and phone when displayed in a webview, but I would like the images to be clickable, so they can can be loaded in a separate activity, and the user can zoom and such.
I guess I am just not using the proper wording when looking for an answer, because I cannot find anything that relates to this. I am assuming I would have to find the tags on click and capture the url somehow, and then pass it to another activity. I am not sure if this is the best way to do this or not. Any insight on this would be greatly appreciated.
web = (WebView) findViewById(R.id.WebView01);
final String mimeType = "text/html";
final String encoding = "UTF-8";
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setScrollbarFadingEnabled(false);
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
web.loadDataWithBaseURL("", product.getString(TAG_CONTENT), mimeType, encoding, "");
This code runs within a Async task that queries the database for info.
Since it is HTML, you can use the onclick attribute of the img tag
<img src="myimage.png" onclick="javascript:window.location=this.src;" />
This will open the image up as the current window.
Or you can do something similar to this answer and send the URL to another activity.
Please help me solve this file path conflict:
As you know, many HTML pages use relative paths starting with a "/" for the href attribute of link tags. For example: link href="/links/style.css".
In my code, I'm using loadDataWithBaseURL for a WebView to set a relative path name. If I give like this:
String webContent=//whole html page;
mWebView.loadDataWithBaseURL("file:///sdcard/mibook/pg90/",new String(webContent), "text/html", "UTF-8","" );
Result: No effect in WebView because (I felt) it takes two '/' while appending to pathname.
If I edit my HTML page by removing the first "/" from the href tag, then the WebView renders properly.
But my problem is that I don't want to edit HTML content as above. Any solution?
Javadoc of loadDataWithBaseURL states:
Note for post 1.0. Due to the change in the WebKit, the access to asset files through
"file:///android_asset/" for the sub resources is more restricted. If you provide null
or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is
anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset
files for sub resources
So basically only allowed URL for file:/// scheme is file:///android_asset/ where files are in your asset folder.
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.