【问题标题】:How to load images from internal storage in webview in android?如何在android的webview中从内部存储中加载图像?
【发布时间】:2015-12-23 10:06:37
【问题描述】:

我们想在 webview 中离线(没有互联网连接)显示 html 页面及其内容,如文本、图像等。我们只能显示文本。对于图像,我们将图像从图像 url 存储在内部存储(sd 卡)上,并将该图像 url(服务器 url)替换为图像内部存储(sd 卡)路径。

但是,这些图像没有显示在 webview 中。

例如, 下面是html中的img标签..

<img alt="img" class="center_top_img" src="http://test.com/uploads/section/images/523.jpg" /> 

我们将上面的图片url(服务器url)替换为图片内部存储(sd卡)路径

<img alt="img" class="center_top_img" src=\"file:///data/data/com.app.test/files/523.jpg\" /> 

【问题讨论】:

  • 您检查替换路径是否有效?即文件存在于同一路径上或不存在一次检查。如果有效,则使用基本 URL 加载数据:==> webView.loadDataWithBaseURL("", html, "text/html","utf-8", ""); stackoverflow.com/a/5055471/2983864
  • 是.. 路径有效。文件存在那里。我们已经替换了这样的网址 并且还使用了 webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");但它不起作用..
  • 你能分享你的代码吗?
  • 使用 Environment.getExternalStorageDirectory() 路径,图像显示在 webview 但使用 getFilesDir() 时,图像存储在 data/data/com.yourapp/files/ 路径中但不显示..让我知道我们是否做错了什么。

标签: android html image webview offlineapps


【解决方案1】:

无论图片在哪里,只要获取它的绝对路径,提前加上“file://”即可。

File file = ...
String imagePath + "file://" + file.getAbsolutePath();
String html = "...<img src=\""+ imagePath + "\">...";

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 感谢您的回答.. 但它不起作用.. 我们已经替换了这样的网址
    【解决方案3】:

    请记住,您的图像位于应用程序目录中。 你可以使用这个:

    <img src="file:///data/data/com.yourapp/files/yourimage.jpg" />
    

    【讨论】:

    • 感谢您的回答.. 使用 Environment.getExternalStorageDirectory() 路径,图像在 webview 中显示,但在使用 getFilesDir() 时,图像存储在 data/data/com.yourapp/files/ 路径中,但是它没有显示..如果我们做错了什么,请告诉我。
    【解决方案4】:

    在我的情况下,我希望图像位于“内部存储”中,而不是您在描述中所述的 sd 卡(并且还从您对 getExternalStorageDirectory 的使用中推断出 - 您的问题标题却提到了“内部存储”。为此原因,找对路:

    //image paths; 'images' folder was earlier created by getFilesDir
    String basePATH = "";
    String imagePSPPath = "";
    String imagePSP = "passportImage.jpeg"; //can be generated dynamically if needed
    File images = new File(getApplicationContext().getFilesDir(), "images");
    if(file.exists()) {
        //fine, it exists, build right strings
        basePATH = images.toString();
        imagePSPPath = basePATH +"/"+ imagePSP;
        Log.i("XPATH", imagePSPPath);   //loged for ease of copying
    }else {
       //error, path not found
        Toast.makeText(YourActivity.this, "Sorry, path not found: ", Toast.LENGTH_LONG).show();
    }
    

    接下来,使用这个路径,但也要确保它有 3 个正斜杠,而不是 2 个。对我来说,我只是从 LOG.i 复制 XPATH 地址并直接粘贴到我的 Html 页面中:

    <img src="file:///"+imagePSPPath +" alt="PSP Photo">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多