Load the image saved in sdcard in webview

2019-01-07 13:58发布

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

3条回答
仙女界的扛把子
2楼-- · 2019-01-07 14:23
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

查看更多
我只想做你的唯一
3楼-- · 2019-01-07 14:29

I tried the methods mentioned before with no success, so this are the options that worked for me to load an image from the external storage:

Load the image directly into the WebView.

Supossing that i have an image called image.jpg inside the root of the external storage directory (in my case /storage/emulated/0/image.jpg) .

  String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
  String imagePath = pathExternalStorage + "/" + "imagen.jpg";

    /*

    //We can chek if the file really exists. 
    File archivo = new File(imagePath);
    if(archivo.exists()){
        Log.i("TAG" , "EXISTS " + imagePath);
    }else{
        Log.e("TAG" , "DOESN´T EXISTS " +imagePath );
    }
    */

  String imagePath = "file://" + imagePath;
  webView.loadUrl(imagePath);

Loading the image using a html template to load into theWebView.

 String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
 String imagePath = pathExternalStorage + "/" + "image.jpg";

String imagePathWV = "file://" + imagePath;
String html = ("<html><head></head><body><img src=\""+ imagePathWV + "\"></body></html>");
webView.loadDataWithBaseURL(null, html, "text/html","utf-8",null);

enter image description here

查看更多
唯我独甜
4楼-- · 2019-01-07 14:36

WebViews can display HTML not images. You either need to use an ImageView or generate some HTML with an image tag that displays your picture. If it needs to be dynamic you can generate it as a String and use the loadData() method to display it.

Edit: You're going to want something like this in your html String.

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");
查看更多
登录 后发表回答