showing image from sdcard with webview not working - android

I have downloaded map750.png file in the root of sdcard, but when I try to show it within a webview with some text, only the text is shown. Could you help me to find what is wrong in the code? thanks.
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String html = "<html><head></head><body><p>hello</p><img src=\"file://mnt/sdcard/map750.png\" alt=\"alternativo\" /></body></html>";
mWebView.loadData(html, "text/html","utf-8");
I edit the post to add the solution suggested by oneilse14, thanks!!
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String html = "<html><head></head><body><p>hello</p><img src=\"file://mnt/sdcard/map750.png\" alt=\"alternativo\" /></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");

Try this,
final String fileName = "file:///mnt/sdcard/1.jpg";
final String mimeType = "text/html";
final String encoding = "utf-8";
final String html = "<img src=\""+fileName+"\">";
webView.loadDataWithBaseURL("", html, mimeType, encoding, "");

Check out loadDataWithBaseURL() in the Developer docs
http://developer.android.com/reference/android/webkit/WebView.html
loadData() has certain restrictions on what it can display. This method is able to display local device files like you are trying to do.

Note that the access using the file:// schema can fail due security restrictions.
Another approach is to use the URI of a ContentProvider that handles the local file access. Overwrite openFile(Uri, String) in a subclass of ContentProvider for this approach.

Related

showing http images from html using webview

i want to show the images from http not stored in local. am not able to show images.
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body>"
+ "<h1>Hello, WebView</h1>"
+"<img src="+"'http://dreamatico.com/data_images/flowers/flowers-5.jpg'"+" height="+142+" width="+142+">"
+ "</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
Create the html file and put it in the assets folder and load that html file using
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/new.html");

How to open locally saved PDF file in an Android WebView?

I have tried to open a locally saved PDF file in a WebView. I can open it by using IntentService, but I want to open it in a WebView. Except for PDFs, all other files are working for me.
I have used the codes below:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setAllowFileAccess(true);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mFilePath));
webView.loadData(mFilePath, mimeType, "UTF-8");
Can anyone please help? Thanks in advance.
You can use Google Docs Viewer to read your pdf online:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
You can also use iframes.
pdfView.setWebChromeClient(new WebChromeClient());
WebSettings settings = pdfView.getSettings();
settings.setPluginState(WebSettings.PluginState.ON);
pdfView.setWebViewClient(new WebViewClient());
pdfView.setBackgroundColor(0x00000000);
settings.setAllowFileAccess(false);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);
String html = "<iframe src=\"http://docs.google.com/gview?url=" +
path + "&embedded=true&wmode=opaque\"" +
"style=\"width:600px; height:900px;\" scrolling=\"no\"" +
"frameborder=\"0\" allowfullscreen></iframe>";
pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);

show image in webpage in android

WebView wv = (WebView) findViewById(R.id.webview1);
wv.setWebViewClient(new Callback());
WebSettings webSettings = wv.getSettings();
webSettings.setBuiltInZoomControls(true);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = "<H1>A simple HTML page</H1><body>" +"<a>link using anchor tag</a>"+
"<p>The quick brown fox jumps over the lazy dog</p>";
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
after link, I want so show image. so
how to put address of that image if it is stored in /images folder of sdcard
i.e. what should be address in src attribute?
regards
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", "");
In a imagePath , set location of your image. Like here its showing test.jpg file is stored in images folder.
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/images/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");

WebView loading images from phone gallery

I'm trying to have the user pick an image from the phone's gallery. Then using the webview to display the image. The file path retrieved is something like "content://media/external/images/media/14". when i looked at other problems, seems like their file path usually start with "file://"
When the program is ran, it displays a long list of random characters. Not sure whats wrong, help is appreciated. thank you
edit this has been solved thx to Md Abdul Gafur's link.
new fixed code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//message is the path of the file.
setContentView(R.layout.activity_picture_large);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
String html = "<html><head></head><body><p>hello</p><img src=\"" + message + "\" alt=\"alternativo\" /></body></html>";
myWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
}

How do i reference an Image from with in an HTML page locally package in an android App?

I was trying to display a static html page in an android app having some images in it.
I saved the image in assests folder and also in res/raw folder.
I was loading html file with this technique and html file is also in res/raw folder.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
String data = ResourceUtils.loadResToString(R.raw.temp, this);
final String mimetype = "text/html";
final String encoding = "UTF-8";
mWebView.loadData(data, mimetype, encoding);
I tried different ways to refer to image like.
img1.png, /img1.png, file:///img1.png, content://img1.png
HtmlViewer/assets/img1.png, content:///HtmlViewer/assets/img1.png and file:///HtmlViewer/assets/img1.png
PackageName/assets/img1.png, content:///PackageName/assets/img1.png and file:///PackageName/assets/img1.png
Can anyone tell me how to get path for image?
file:///android_asset/file_name.ext
This should work. Use this method instead.
loadDataWithBaseURL("file:///android_asset/", summary, "text/html", "utf-8", null);

Categories

Resources