Display image from SD card with idle web browsing

I uploaded the map750.png file to the sdcard root directory, but when I try to show it in a webview with some text, only the text is displayed. Could you help me 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 am editing a 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", ""); 
+4
source share
3 answers

See loadDataWithBaseURL () in the Developer Docs

http://developer.android.com/reference/android/webkit/WebView.html

loadData () has certain limitations on what it can display. This method is able to display the files of local devices that you are trying to make.

+2
source

Try it,

  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, ""); 
+5
source

Please note that access using the file: // scheme may cause a crash due to security restrictions.

Another approach is to use the ContentProvider URI, which handles access to the local file. Rewrite openFile (Uri, String) in a subclass of ContentProvider for this approach.

0
source

Source: https://habr.com/ru/post/1383156/


All Articles