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 conta开发者_运维问答ins 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);
I have made a tutorial on how to use the loadDataWithBaseURL() method in order to display images - http://lomza.totem-soft.com/tutorial-using-webview-to-load-the-html-page-from-assets/#header
For example, if you want to use some images from the sdcard, then your code should be like this:
final String path = Environment.getExternalStorageDirectory() + File.separator + "YourFolderName");
bookView.loadDataWithBaseURL("file://" + path, htmlTextWithHeadAndBody, "text/html", "UTF-8", "");
精彩评论