【问题标题】:Images are not displayed while loading html file in android web view在android web视图中加载html文件时不显示图像
【发布时间】:2013-03-22 13:45:06
【问题描述】:

我需要加载一个 html 文件,它将在 android 模拟器中显示图像。我有一个单独的 css 文件和 html fie 以及要在我的应用程序的 /assets 文件夹中显示的图像。我使用 getAssets() 读取了 css 文件和 html 文件。我尝试使用 loadData() 方法加载 html 文件,因为我只能使用字符串来获取字符串中的 html 文件,并且仅使用该字符串而不是使用“file:///android_asset”来加载 html 文件/eppi.html”。谁能说出解决方案?。您可以从我的以下代码中理解。 提前致谢。

我的 MainActivity.java 代码:

 package com.exercise.AndroidHTML;

 import java.io.InputStream;
 import java.io.IOException;

 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.os.Bundle;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.content.res.AssetManager;


 public class AndroidHTMLActivity extends Activity {

    WebView myBrowser;
String html;
String css;
String HTML;

 /** Called when the activity is first created. */
 @SuppressLint("SetJavaScriptEnabled")
@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myBrowser = (WebView)findViewById(R.id.mybrowser);

    AssetManager assetmanager=getAssets();
    InputStream input;
    try{
        input= assetmanager.open("eppi.html");

        int size=input.available();
        byte[] buffer=new byte[size];
        input.read(buffer);
        input.close();

         html=new String(buffer);



    } catch (IOException e){
        e.printStackTrace();
    }
    try{
        InputStream input1= assetmanager.open("eppi.css");

        int size=input1.available();
        byte[] buffer=new byte[size];
        input1.read(buffer);
        input1.close();

         css=new String(buffer);
    }catch (IOException e){
        e.printStackTrace();
    }

     HTML=css+html;     
    myBrowser.getSettings().setJavaScriptEnabled(true);
    WebSettings settings = myBrowser.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
    myBrowser.loadData(HTML,"text/html","utf-8");


 }
 }

我的 eppi.css 文件:

<html>
<head>
<style>
#header{
height:80px;
width:320px;
position:absolute;
background-color:#000000;
}
#text{
height:80px;
width:159px;
position:absolute;
color:#806C00;
}
#logo{
top:2px;
    left:200px;
height:80px;
width:97px;
position:absolute;

}
#image{
top:79px;
height:80px;
width:159px;
position:absolute;

}
#image1{
top:79px;
left:159px;
height:80px;
width:161px;
position:absolute;

}
#image2{
top:158px;
height:80px;
width:159px;
position:absolute;

}
#image3{
top:158px;
left:159px;
height:80px;
width:161px;
position:absolute;

}
#body{
margin:0;
padding:0;
}

</style>
</head>

我的 eppi.html 代码:

<body id="body">
<div id="header">
<div id="text">
<center>
h4><b><i>The Show
Welcomes You</i></b></h4>
</center>
</div>
<div id="logo">
<img src="logo.png" width="100" height="75">
</div>
</div>
<div id="image">
<a href="Media.html"><img src="mine.png" width="159" height="80"/></a>
</div>
<div id="image1">
<a href="Trading.html"><img src="offers.png" width="161" height="80"/></a>
</div>
<div id="image2">
<a href="Hours.html"><img src="horse.png" width="159" height="80"/></a>
</div>
<div id="image3">
<a href="Release.html"><img src="entering.png" width="161" height="80"/></a>
</div>
</body>
</html>

【问题讨论】:

  • 也许你需要一个 www 文件夹,见this
  • 我认为这是同一个问题,见this

标签: java android html


【解决方案1】:

您可以解析 HTML(至少在初级阶段)、查找图像并手动加载它们(就像处理 HTML 文件一样)。

您可以将 HTML 中的图像 URL 更改为 base64 回显的data URLs。

【讨论】:

  • 谢谢。我按照你说的试过了。但这对我不起作用
  • @Meena 请提供一些细节。你被困在哪里了?这只是一个调试问题,因为我确信 Android 浏览器支持数据 URL。
【解决方案2】:

您必须更改图像路径。而不是 写成

【讨论】:

    猜你喜欢
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多