I'm trying to display the content of a html file with the loadDataWithBaseURL() method in android.
I just have a String that contains the Html file data in one String called source and I then pass this to the method.
for e.g
String source; //contain html tags with images
View.loadDataWithBaseURl(null,source,"text/html","UTF-8","about:blank");
The data displayed in view is fine. My problem is if my html file contains any images then I couldn't displayed it? how can I do that?
you can do it, if the images in the source use relative locations for the src then you need to set the baseUrl to the "base" of where the images would be located. for example, if you were loading google's home page from the source, it would look like this:
View.loadDataWithBaseURI("http://google.com",source,"text/html","UTF-8","about:blank");
That tells the webview where the images will be loaded from.
As a side note, I do not think "file://" URIs works in the web view, for security reasons.
use "file:///android_res/raw/" as your base URL and put your files in res/raw in your project.
res/raw/index.html
res/raw/image.jpg
InputStream htmlStream = getResources().openRawResource(R.raw.index);
Reader is = new BufferedReader(new InputStreamReader(htmlStream, "UTF8"));
// read string from reader
String html = readFile(is);
webView.loadDataWithBaseURL("file:///android_res/raw/", html,
"text/html", "UTF-8", null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With