Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display image with loadDataWithBaseURL() method in android?

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?

like image 763
user370305 Avatar asked Jun 18 '10 13:06

user370305


2 Answers

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.

like image 86
Ryan Conrad Avatar answered Oct 12 '22 23:10

Ryan Conrad


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);
like image 36
Ken Goodridge Avatar answered Oct 12 '22 22:10

Ken Goodridge