java - 在 android web View 中加载 html 文件时不显示图像

标签 java android html

我需要加载一个 html 文件,它将在 android 模拟器中显示图像。我有一个单独的 css 文件和 html fie 以及要在我的应用程序的/assets 文件夹中显示的图像。我使用 getAssets() 读取了 css 文件和 html 文件。我尝试使用 loadData() 方法加载 html 文件,因为我在这种情况下只使用字符串来获取字符串中的 html 文件,并且只使用该字符串加载 html 文件,而不是使用“file:///android_asset/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>

最佳答案

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

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

关于java - 在 android web View 中加载 html 文件时不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15571894/

相关文章:

html - 有没有办法解决 IE10&9 更宽的字体渲染差异?

html - 如何创建圆形图表?

java - 在选项卡更改后(使用支持库),先前分离的 fragment 在当前 fragment 后面保持可见

java - 使用 Chef Java Recipe 时如何访问 java_home

android - 确定 Android samsung Galaxy 选项卡中我的应用程序发生了什么情况

java - Android 中单顶模式的 Intent 附加功能

Android Oreo 不播放自定义通知声音

java - 优化 GC 运行时间

java - 如何从 .erb 文件中的函数调用中提取函数名称?

javascript - 检测Chrome浏览器窗口宽高,防止元素溢出