Android:在webview中读取ISO-8859-1字符编码的文本文件

Android:在webview中读取ISO-8859-1字符编码的文本文件,android,encoding,webview,Android,Encoding,Webview,主题说明了一切。 我有一个html文件,用ISO-8859-1编码(用Firefox检查编码) 代码以字符串的形式正确读取文件,但当我试图在WebView中显示文本时,会出现奇怪的字符,而不是a,è 我尝试了很多代码(我想除了好的代码)。 最新版本如下(阅读后): 我想你需要把txt文件读作byte[],然后使用新字符串(bytes,“ISO-8859-1”)将其编码为字符串,然后再显示它好的,我自己回答。此代码适用于: try { InputStream

主题说明了一切。 我有一个html文件,用ISO-8859-1编码(用Firefox检查编码)

代码以字符串的形式正确读取文件,但当我试图在WebView中显示文本时,会出现奇怪的字符,而不是a,è

我尝试了很多代码(我想除了好的代码)。 最新版本如下(阅读后):


我想你需要把
txt
文件读作
byte[]
,然后使用
新字符串(bytes,“ISO-8859-1”)
将其编码为
字符串,然后再显示它

好的,我自己回答。此代码适用于:

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){
        }
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){
        }

问题在于,这只是一个示例,在实际程序中,我将从sqlite数据库接收数据。我也有同样的问题(数据是ISO-8859-1编码的)。如果我检查txt变量,数据和编码是正确的。当我试图在webview中显示以txt格式存储的html页面时,就会出现问题。然后尝试使用
webview.loadData(testo,“text/html;charset=UTF-8”,“ISO-8859-1”)
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){
        }