Jack Hessel
Jack Hessel

Reputation: 103

Android Text File Importing

I'm trying to import a text file to simply display in a textview using android.

The file is called "legal" and is located in res/raw

I use the following code to read in the file...

        BufferedReader bufferreader = new BufferedReader(new
        InputStreamReader(getResources().openRawResource(R.raw.legal), Charset.forName("UTF8")));
        String bufferLine;
        String tempText = "";
        try
        {
            while ((bufferLine = bufferreader.readLine()) != null)
            {
                tempText = tempText + bufferLine + "\n";
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

When I try to display tempText in a TextView, directional "smart" quotes are displayed as as unknown characters. The text file, legal, is encoded with UTF8 (used notepad++). I can't seem to figure out what the problem is.

Upvotes: 2

Views: 1276

Answers (1)

JustDanyul
JustDanyul

Reputation: 14044

Are you sure "UTF8" is a valid charset name? the documentation for Charset seems to suggest UTF-8

I usually just do

InputStreamReader(inputStream, "UTF-8")

and haven't had any problems. I have had problems with utf encoded html style. As in &#UNICODENUMBER;

This fixed those problems

txtView.setText(Html.fromHtml(str).toString());

EDIT

Since the above didn't help you I would try the following:

txtView.setText(Html.fromHtml("“" + " Hello World " + "”").toString());

Just to see if the font used has support for the characters (I'm guessing you are after curling quotes). If it does render, there must be a issue with either the way you read the file (even-though, it looks valid enough), or the actual file.

Hope that helps :)

Upvotes: 1

Related Questions