如何从android的webview的内部存储中加载图像?

问题描述 投票:3回答:3

我们想在网页视图中离线显示html页面,其内容如文本,图像等(不连接互联网)。我们只能显示文本。对于图像,我们将图像URL中的图像存储在内部存储(SD卡)中,并用图像内部存储(SD卡)路径替换该图像URL(服务器URL)。

但是,这些图像未显示在网络视图中。

例如,下面是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\" /> 
android html image webview offlineapps
3个回答
1
投票

[请记住,您的图片在应用目录中。您可以使用此:

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

0
投票

尝试一下

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

0
投票

无论图像在哪里,只要获取其绝对路径并预先添加“ file://”。

File file = ...
String imagePath + "file://" + file.getAbsolutePath();
String html = "...<img src=\""+ imagePath + "\">...";
© www.soinside.com 2019 - 2024. All rights reserved.