4

I have HTML file in assets folder which is encoded in UTF8(contain Persian characters), I want to read this file and load it into a TextView.I read lots of posts like load utf-8 text file , load HTML file into TextView , read UTF8 text file from res/raw and write this code:

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.");
    }

But It show incorrect characters! I also try to convert total.toString() into a UTF8 String array and then add it to textView but it didn't work too

textView.setText(Html.fromHtml(new String(total.toString().getBytes("ISO-8859-1"), "UTF-8")));

There is no problem with textView or emulator because when I load HTML from Database, It shows utf8 characters correctly! So what should I do?

Community
  • 1
  • 1
Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67

1 Answers1

0

After lots of searching and test some other codes,at the end I replace my HTML file with another one.Surprisingly my code works fine! I investigate former HTML file and notice that it has Unicode encoding!!! So if you have a same problem, first of all check your file's encoding and make sure that it is correct.

Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67
  • Note that the encoding Windows misleadingly calls “Unicode” is actually UTF-16LE. This encoding uses 2-byte code units and isn't compatible with ASCII. It's generally not a good choice for web content; UTF-8 is much more practical. – bobince Dec 08 '14 at 22:44