Android : 在 Webview 中显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10700998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : Display images in Webview
提问by Minh Nguyen
After processing a file, I get a HTML string in which the image is set as
处理文件后,我得到一个 HTML 字符串,其中图像设置为
<img src="abc.001.png" width="135" height="29" alt="" style="margin-left:0pt; margin-top:0pt; position:absolute; z-index:-65536" />
The path of the image should not be modified because I have to choose the file item from a list. The image is in the same directory as the file. I load the HTML string using loadData/loadDataWithBaseURL, but the image isn't displayed. I only see its frame.
不应修改图像的路径,因为我必须从列表中选择文件项。图像与文件位于同一目录中。我使用 loadData/loadDataWithBaseURL 加载 HTML 字符串,但未显示图像。我只看到它的框架。
How can I fix this? And can I apply that solution in case I have many images which are indexed as .001.jpg, .002.png , etc... (all in a directory) ?
我怎样才能解决这个问题?如果我有很多图像被索引为 .001.jpg、.002.png 等...(都在一个目录中),我可以应用该解决方案吗?
Update: Thanks, it works with loadUrl() statement no matter how I name the image. In fact I have to read and process the content before loading it in WebView. That's why I use loadDataWithBaseUrl() statement and get the trouble above. Here's my code in the test project to read and display the content of Test.html.
更新:谢谢,无论我如何命名图像,它都适用于 loadUrl() 语句。事实上,在将内容加载到 WebView 之前,我必须阅读和处理内容。这就是为什么我使用 loadDataWithBaseUrl() 语句并遇到上述问题的原因。这是我在测试项目中读取和显示Test.html内容的代码。
String res = "";
File file = new File(Environment.getExternalStorageDirectory()+"/Test.html");
try {
FileInputStream in = new FileInputStream(file);
if (in != null) {
BufferedReader buffreader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = buffreader.readLine()) != null) {
res += line;
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
//wv.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Test.html");
The statement in // works but that's not what I can do in my real project. I have a solution: after processing the content I have to save it in a temporary HTML file then load it, that file will be delete later. However, I'm still looking forward to a better solution :)
// 中的语句有效,但这不是我在实际项目中可以做的。我有一个解决方案:处理完内容后,我必须将其保存在一个临时的 HTML 文件中,然后加载它,该文件稍后将被删除。但是,我仍然期待更好的解决方案:)
回答by Ponmalar
Try to change the name of the image file. I thought this is because of double dot in the name.
尝试更改图像文件的名称。我认为这是因为名称中的双点。
<img id="compassRose" src="CompassRose.jpg"></img>
this is working for me....
这对我有用....
Code:
代码:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
public class StackOverFlowActivity extends Activity {
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView view=(WebView)findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/index.html");
view.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
}
final class MyJavaScriptInterface
{
public void ProcessJavaScript(final String scriptname, final String args)
{
mHandler.post(new Runnable()
{
public void run()
{
//Do your activities
}
});
}
}
}
index.html:
索引.html:
<html>
<head>
<title title="Index"></title>
</head>
<body>
<h2>Android App demo</h2>
<br /> <img src="CompassRose.jpg" />
</body>
</html>
Result:
结果:
回答by DanC
Your problem is with the line:
你的问题出在这条线上:
wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
The first parameter (baseURL) is null. For some reason, WebView then refuses to load any linked resources even if you use absolute URLs. You might find that it will work if you change your code to (assuming your images are stored in the external dir):
第一个参数 (baseURL) 为空。出于某种原因,即使您使用绝对 URL,WebView 也会拒绝加载任何链接的资源。如果您将代码更改为(假设您的图像存储在外部目录中),您可能会发现它会起作用:
wv.loadDataWithBaseURL ("file://" + Environment.getExternalStorageDirectory(), res, "text/html", "utf-8", null);
Remember to add the proper permission if you refer to resources on the network:
如果您引用网络上的资源,请记住添加适当的权限:
<uses-permission android:name="android.permission.INTERNET" />
Sorry, a little late in my reply. I was researching a similar problem when I came across this post.
抱歉,我的回复有点晚了。当我遇到这篇文章时,我正在研究类似的问题。
回答by Ali Ziaee
simply use:
只需使用:
webview.loadUrl("file:///android_asset/mypicture.jpg");
回答by Abhisek
WebView webView=(WebView)findViewById(R.id.my_wb);
String url = "YOUR URL";
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(url);