安卓 : Display images in Webview

标签 android image webview

处理文件后,我得到一个HTML字符串,其中图像设置为

<img src="abc.001.png" width="135" height="29" alt="" style="margin-left:0pt; margin-top:0pt; position:absolute; z-index:-65536" />

不应修改图像的路径,因为我必须从列表中选择文件项。图像与文件位于同一目录中。我使用 loadData/loadDataWithBaseURL 加载 HTML 字符串,但未显示图像。我只看到它的框架。

我该如何解决这个问题?如果我有许多索引为 .001.jpg、.002.png 等的图像(都在一个目录中),我可以应用该解决方案吗?

更新:谢谢,无论我如何命名图像,它都适用于 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");

//中的语句有效,但这不是我在实际项目中可以做的。我有一个解决方案:处理完内容后,我必须将其保存在一个临时 HTML 文件中然后加载它,该文件稍后将被删除。但是,我仍然期待更好的解决方案:)

最佳答案

尝试更改图像文件的名称。我认为这是因为名称中有双点。

<img id="compassRose" src="CompassRose.jpg"></img>

这对我有用....

代码:

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>
<head>
  <title title="Index"></title>                                   
</head>

<body>
  <h2>Android App demo</h2>
  <br /> <img src="CompassRose.jpg" />
</body>
</html>

enter image description here

结果:

enter image description here

关于安卓 : Display images in Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10700998/

相关文章:

Cocoa WebView导航白闪

java - 手动禁用和启用 Android HostApduService?

html - 主图库图像未居中

android - 在不使用 WebView 的情况下在 ListView 中对齐 android 文本

java - PDFBox:将pdf页面转换为图像的问题

python - 在 Python Tkinter 中使用函数时添加图像

java - 使用 LoadData 时在 WebView 中运行 Javascript

android - 使用数据库适配器从 ListView 中删除项目

java - 从 android listView 中删除项目

android - PendingIntent 启动应用程序包的不同 Activity