我需要在Android模拟器中加载一个HTML文件,其中会显示图片。我有一个单独的CSS文件和HTML文件以及要显示在应用程序的/assets文件夹中的图像。我使用getAssets()方法读取CSS文件和HTML文件。我尝试使用loadData()方法加载HTML文件,因为我只能使用字符串获取HTML文件,并且只能通过使用该字符串来加载HTML文件,而不能使用"file:///android_asset/eppi.html"。有人能说出解决方案吗?您可以从我的以下代码中了解到这一点。
感谢您的提前帮助。
我的MainActivity.java代码:
感谢您的提前帮助。
我的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>