【问题标题】:showing image from sdcard with webview not working使用 webview 显示来自 sdcard 的图像不起作用
【发布时间】:2012-01-06 18:07:17
【问题描述】:

我已经在 sdcard 的根目录中下载了 map750.png 文件,但是当我尝试在带有一些文本的 web 视图中显示它时,只显示文本。你能帮我找出代码中的问题吗?谢谢。

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

我编辑帖子以添加oneilse14建议的解决方案,谢谢!!

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

【问题讨论】:

  • 检查我的答案,它肯定会工作。
  • 如何从 sdcard 加载 SVG 图像?

标签: android image webview sd-card


【解决方案1】:

试试这个,

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

【讨论】:

  • 如何从 sdcard 加载 SVG 图像?
【解决方案2】:

查看开发者文档中的 loadDataWithBaseURL()

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

loadData() 对它可以显示的内容有一定的限制。此方法能够像您尝试的那样显示本地设备文件。

【讨论】:

    【解决方案3】:

    请注意,由于安全限制,使用 file:// 架构的访问可能会失败。

    另一种方法是使用处理本地文件访问的 ContentProvider 的 URI。在 ContentProvider 的子类中为这种方法覆盖 openFile(Uri, String)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      相关资源
      最近更新 更多