Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将带有UTF8编码的Html文件从资源加载到文本视图中_Java_Android_Unicode_Encoding_Utf 8 - Fatal编程技术网

Java 将带有UTF8编码的Html文件从资源加载到文本视图中

Java 将带有UTF8编码的Html文件从资源加载到文本视图中,java,android,unicode,encoding,utf-8,Java,Android,Unicode,Encoding,Utf 8,我在assets文件夹中有一个HTML文件,它是用UTF8编码的(包含波斯字符),我想读取该文件并将其加载到TextView中。我读了很多文章,如,,并编写了以下代码: try{ InputStream inputStream = getResources().getAssets().open("htmls/salamati.html"); // I also try "UTF-8" but none of them wor

我在assets文件夹中有一个HTML文件,它是用UTF8编码的(包含波斯字符),我想读取该文件并将其加载到TextView中。我读了很多文章,如,,并编写了以下代码:

try{
        InputStream inputStream = getResources().getAssets().open("htmls/salamati.html");
        // I also try "UTF-8" but none of them worked
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream,"UTF8"));
        StringBuilder total = new StringBuilder();
        String html;
        while ((html = r.readLine()) != null) {
            total.append(html);
        }
        // total contains incorrect characters
        textView.setText(Html.fromHtml(total.toString()));
    }
    catch (IOException exception)
    {
        textView.setText("Failed loading HTML.");
    }
但它显示的字符不正确! 我还尝试将total.toString()转换为UTF8字符串数组,然后将其添加到textView中,但效果不佳

textView.setText(Html.fromHtml(new String(total.toString().getBytes("ISO-8859-1"), "UTF-8")));
textView或emulator没有问题,因为当我从数据库加载HTML时,它会正确显示utf8字符!
那我该怎么办呢?

在大量搜索和测试其他代码之后,最后我用另一个HTML文件替换了我的HTML文件。令人惊讶的是,我的代码工作得很好!我调查了以前的HTML文件,发现它有Unicode编码!!!
因此,如果您有相同的问题,首先检查文件的编码并确保其正确。

请注意,编码窗口错误地调用“Unicode”实际上是UTF-16LE。此编码使用2字节代码单位,与ASCII不兼容。对于web内容来说,它通常不是一个好的选择;UTF-8更实用。