Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load the image saved in sdcard in webview

Tags:

The below code is used

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = base + "/test.jpg";  
mWebView.loadUrl(imagePath);

the image is not loading ...

also tried

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");

Please help

like image 282
xydev Avatar asked Feb 19 '11 14:02

xydev


People also ask

How do I open a PDF file from internal storage in WebView Android programmatically?

All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

Which method is used to load HTML content in WebView?

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.


1 Answers

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");  

This did the trick as we have to append the"prefix "file://" before any file so as to display in the webview

like image 79
xydev Avatar answered Sep 30 '22 18:09

xydev