【问题标题】:Displaying Image from local android sd card to WebView将图像从本地 android sd 卡显示到 WebView
【发布时间】:2018-03-19 11:48:08
【问题描述】:

我想知道是否有任何方法可以将本地图像从 Android sd 卡显示到 WebView?

我检查了这个site 和类似的问题,但他们都说你可以使用loadDataWithBaseUrl。但都没有达到我的预期,例如:

String html = new String();
    html = ("<html><body><header><h1>Title</h1></header><section><img src=\""+URI_PREFIX+IMAGE_FILENAME+"\" align=left><p>Content</p></section></body></html>" );

    web.loadDataWithBaseURL(URI_PREFIX,
    html,
    "text/html",
    "utf-8",
    "");

它从 Java (Android) 创建一个 html,我不希望它是那样的。我希望它只加载 URL(例如 http://example.com)并将本地图像从 Android sd 卡传递到 http://example.com。我想要这样,因为URL会超时更改,如果我每次需要更改html时都必须更改我的Android App。

用例是: 在 webview / website 中,我将调用 Android 的函数来打开相机并拍照(直到这里都很好)。问题来了,图片保存到Android本地sd卡,我需要在webview/网站上显示图片,到目前为止我还没有找到这个用例的答案。

需要你的帮助,谢谢

【问题讨论】:

  • src=\""+URI_PREFIX+IMAGE_FILENAME+"\" ???我们应该知道你想做什么吗? Please explain your code – Billa.
  • 我错过了 URI_PREFIX 变量,假设该变量是 android 中本地存储的路径 (file:///data/data//)。它会将图像从 Android 本地存储显示到 webview

标签: javascript java android webview android-webview


【解决方案1】:

1.按照下面的代码。

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

【讨论】:

  • getExternalStorageDirectory().getAbsolutePath() 获取SD卡所在的系统部分。结合base和系统部分我们可以得到图片所在的图片路径。
  • 你的解决方案和我发布的代码有什么区别?
猜你喜欢
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多