Load UTF8 encoded Html file from assets to TextView

I have an HTML file in a resource folder that is encoded in UTF8 (contains Persian characters), I want to read this file and load it into a TextView.I have read a lot of messages, like load a utf-8 text file , load an HTML file into a 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 is showing the wrong characters! I am also trying to convert total.toString () to a UTF8 string array and then add it to the textView, but it doesn't work either.

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

      

No problem with textView or emulator because when I load HTML from database it renders utf8 characters correctly! So what should I do?

+3


source to share


1 answer


After a lot of searching and testing some other codes, at the end I replace my HTML file with another one. Surprisingly, my code works great! I examine the old HTML file and notice that it encodes Unicode !!! Therefore, if you have the same problem, first of all check the file encoding and make sure it is correct.



0


source







All Articles