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.
Related
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)
In my local storage I have the following folder
/data/data/com.test.testapp/files/1/images
and the following file exists.
/data/data/com.test.testapp/files/1/images/f.png
These were stored/retrieved using context.getApplicationContext().getFilesDir()
In my HTML I have "<img src="f.png" />"
I can get TextView working with html/images using an ImageGetter, which effectively returns a drawable from:
Drawable.createFromPath(targetFile.getAbsolutePath());
Where the targetFile is as described above ends up being (/data/data/com.test.testapp/files/1/images/f.png)
However, when I try to use WebViewer as follows (I've pasted in the string for ease):
String url = "file:///data/data/com.test.testapp/files/1";
webViewer.loadDataWithBaseURL(url, content, "text/html", "utf-8", null);
Where content ends up with the same text as my TextView.
All I get is an image placeholder, i.e. it didn't find it.
OMG! ;)
How petty are those people at Google!
All I had to do was append a slash to the url so the base url:
file:///data/data/com.test.testapp/files/1
When changed to
file:///data/data/com.test.testapp/files/1/
Works.
I have a html string containing seveal img tags which I am passing to webview's loadDataWithBaseURL method like
String data = "some html with <img> and <link>.....";
wview.loadDataWithBaseURL("http://dummy.baseurl/", data, "text/html", "UTF-8", null);
if I dont pass the first parameter html can be displayed but subsequent requests for or css files are not triggered thats why I am passing a dummy baseUrl.
Running the code when I try to look what requests were made under shouldInterceptRequest() like below
wview.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
Log.d("url="+url, "resources");
....
}
});
the I can see outputs like
http://dummy.host.name/images/face.jpg
etc
but my original html contains ".." in img src like <img src="../images/face.jpg"> trouble is parent directory (..) part is ignored by webview
this ".." is important for me I cannot figure out why it is skipping that part
EDIT
I am loading images from a zip file so inside shouldInterceptRequest() I can put necessary logic but first I should have correct src.
I also noticed that if path is appended to baseUrl, they are also ignored for example
http://dummy.baseUrl/one/two/
becomes (looking from request logs)
http://dummy.baseurl/
I suspect if these two are related !
Where do you want to load the images from? If you're trying to load them from the phone you need to use the assets folder.
https://stackoverflow.com/a/7268695/642161
I resolved my problem using a hack, not sure a better solution exist!
It looks like baseUrl and src are merged to create final request urls so if any ".." present in src it will be used to recalculate final url and they disappear no matter if top level directory exist.
I changed baseUrl to
file:///android_asset/x123_/x123_/x123_/x123_/
Here "x123_" is any random character sequence one can take but should be least likely to appear in "src"
Now count the mumber of "x123_" in request url inside shouldInterceptRequest() if it is 4 (as in this example) there were no ".." in src otherwise number of ".." = 4 - count
I know that there are a lot of questions about to load HTMl-pages into WebView.
What am I doing? I just put down the 1.html in assets folder in android-project and use
myWebView.loadUrl("file:///assets/"+selectedItem+".html");
where
selecteditem
is data from intent. As result I get the message in WebView that:
file:///assets/1.html was not found.
UPD: sorry, I found the solution. the correct path should be the follow:
myWebView.loadUrl("file:///android_asset/"+selectedItem+".html");
The correct path is file:///android_asset/1.html
I am not sure,but it may work:
webView.loadUrl("file:///android_asset/1.html");
Know more about the same type of mistake: Loading existing .html file with android WebView
hi
In my application i need to display a html page in webview and that html page should refer to sdcard location for .css file (Using link href="...." tag).
I placed 2 files (i.e) html and css ) in sdcard->mibook.
I tried by giving absolute path as link href="/mnt/sdcard/mibook/0.css"
how can i specify path name android?
Edited:
Above problem solved : by using this- link href="file:///sdcard/mibook/0.css"
But i have following requirement,
i need to handle around 50+ html file every time ,
I placed all files as follows,
mibook->book1->pg90.sqlite
mibook->book1->links->o.css and 1.css
The sqlite file contains html pages. each html page has css reference as link href="/links/0.css"
These css file should be refer by html. how can i do this?
Try
link href="file:///sdcard/mibook/0.css"
Assuming Android >= 2.1