主题说明了一切。 我有一个html文件,用ISO-8859-1编码(使用Firefox检查编码,如解释here)。
代码在字符串中正确读取文件但是当我尝试在WebView中显示文本时,我会使用奇怪的字符代替à,è,...
我已经尝试了很多代码(除了好的代码,我猜)。 最后一个版本是这个(在阅读this之后):
try
{
InputStreamReader is = new InputStreamReader(getApplicationContext().getAssets().open("file.html"), "ISO-8859-1");
BufferedReader br = new BufferedReader(is);
String txt = "";
while ( (result = br.readLine()) != null)
{
txt= txt+ result;
}
String uri = Uri.encode("<?xml version='1.0' encoding='ISO-8859-1'?>" + txt);
webView.loadData(uri, "text/html", "ISO-8859-1");
}
catch (IOException ex){
}
解
好的,我正在回答自己。此代码有效:
try
{
InputStreamReader is = new InputStreamReader(getApplicationContext().getAssets().open("filelgc2.html"), Charset.forName("ISO-8859-1"));
BufferedReader br = new BufferedReader(is);
String testo = "";
while ( (result = br.readLine()) != null)
{
testo = testo + result;
}
webView.loadData(testo, "text/html; charset=UTF-8", null);
}
catch (IOException ex){
}
答案 0 :(得分:2)
好的,我正在回答自己。此代码有效:
try
{
InputStreamReader is = new InputStreamReader(getApplicationContext().getAssets().open("filelgc2.html"), Charset.forName("ISO-8859-1"));
BufferedReader br = new BufferedReader(is);
String testo = "";
while ( (result = br.readLine()) != null)
{
testo = testo + result;
}
webView.loadData(testo, "text/html; charset=UTF-8", null);
}
catch (IOException ex){
}
答案 1 :(得分:0)
我认为您需要将txt
文件作为byte[]
阅读,然后在显示之前使用String
将其编码为new String(bytes,"ISO-8859-1")