【问题标题】:Load the image saved in sdcard in webview在 webview 中加载保存在 sdcard 中的图像
【发布时间】:2011-06-30 09:32:44
【问题描述】:

使用以下代码

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);

图片未加载...

也试过了

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");

请帮忙

【问题讨论】:

    标签: android image webview


    【解决方案1】:
    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", "");  
    

    这成功了,因为我们必须在任何文件之前附加“前缀“file://”以便在 webview 中显示

    【讨论】:

    • 当我插入 mWebView.loadDataWithBaseURL("", content, "text/html","utf-8", ""); 而不是 mWebView.loadData(html, "text/html","utf-8"); 时,它对我有用
    • 它不起作用.. 我们已经替换了这样的 url 并使用 mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
    • 谢谢你,它就像一个魅力!进入我的 html ""
    • 当我插入 mWebView.loadDataWithBaseURL("", content, "text/html","utf-8", "");而不是 mWebView.loadData(html, "text/html","utf-8");
    • 阅读答案后,我仍然错过了 loadDataWithBaseURL。不确定 loadData 和 this 有什么不同。总之谢谢你
    【解决方案2】:

    WebView 可以显示 HTML 而不是图像。您要么需要使用 ImageView,要么使用显示图片的图像标签生成一些 HTML。如果需要动态,可以将其生成为 String 并使用 loadData() 方法显示。

    编辑:你会在你的 html 字符串中想要这样的东西。

    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");
    

    【讨论】:

    • 您将 HTML 字符串留空。稍后我将编辑我的答案以显示一个示例。
    • 你确定你的镜像路径构建正确吗?在调用 loadData() 之前尝试记录 imagePath 字符串,并确保图像位于它在日志中显示的位置。还可以尝试在 元素内的 html 字符串中添加一些纯文本,以查看是否显示。如果显示文本但不显示图片,则说明文件路径存在问题。
    • @Tim 我会看.. 图像的权限是----rwxr-x .... 如果添加一些文本很容易显示..
    • @Tim String html = 和String imagePath = /mnt/sdcard/test.jpg 登录时
    • @Tim 最后我能够解决我们必须在基本路径之前附加 file:/ 才能将图像加载到 webview
    【解决方案3】:

    我尝试了前面提到的方法但没有成功,所以这是我从外部存储加载图像的选项:

    将图像直接加载到 WebView 中。

    假设我在外部存储目录的根目录中有一个名为image.jpg 的图像(在我的情况下为/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);
    

    使用html模板加载图片以加载到WebView中。

     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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2023-04-02
      • 2018-12-25
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      相关资源
      最近更新 更多