0

I am using ByteArrayOutputStream to put text in a Text View from an IputStream. This works fine but... I am from Sweden and when I put a text with some special Swedish letters it puts ? instead of the actual letter. The system have no problems with this letters otherwise. Hope someone out there can give me a hint about what to do.

Perhaps I shall show the code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
    helloTxt.setText(readTxt());
}

 private String readTxt(){
 InputStream inputStream = getResources().openRawResource(R.raw.hello);
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 int i;
 try {
 i = inputStream.read();
 while (i != -1)
  {
   byteArrayOutputStream.write(i);
   i = inputStream.read();
  }
  inputStream.close();
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}

 return byteArrayOutputStream.toString();
}
}

I also tied this, get it from the forum (Selzier): Nice peace but still no Swedish letters in the output:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = (TextView)findViewById(R.id.txtRawResource);  
    tv.setText(readFile(this, R.raw.saga));
}

private static CharSequence readFile(Activity activity, int id) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(
                activity.getResources().openRawResource(id)));
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = in.readLine()) != null) buffer.append(line).append('\n');
        return buffer;
        } 
    catch (IOException e) {
        return "";
    } 
    finally {
        closeStream(in);
    }
}

/**
 * Closes the specified stream.
 */
private static void closeStream(Closeable stream) {
    if (stream != null) {
        try {
            stream.close();
        } catch (IOException e) {
            // Ignore
        }
    }
}
}
Christer
  • 101
  • 10

1 Answers1

0

You are using the wrong encoding when you read/write the stream. Use UTF-8.

 outputStream.toString("UTF8")

Edit: try this approach posted here. I think it can also be a issue if your file has a BOM. Use NotePad++ or another editor to remove it.

 public static String readRawTextFile(Context ctx, int resId)
 {
     InputStream inputStream = ctx.getResources().openRawResource(resId);

     InputStreamReader inputreader = new InputStreamReader(inputStream);
     BufferedReader buffreader = new BufferedReader(inputreader);
     String line;
     StringBuilder text = new StringBuilder();

     try {
         while (( line = buffreader.readLine()) != null) {
            text.append(line);
            text.append('\n');
         }
     } catch (IOException e) {
         return null;
     }
     return text.toString();
 }
Community
  • 1
  • 1
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
  • so at the end, change `return byteArrayOutputStream.toString();` to `return byteArrayOutputStream.toString("UTF-8");` – TheCodeKing Sep 10 '11 at 23:30
  • The code did not accept this: says:Unhandled exception type UnsupportedEncodingException. – Christer Sep 11 '11 at 06:13
  • Still not accept it, I thought this thing of stuff were included in the package. In the project all xml.files are defined with UTF-8 and I have no trouble with anything else related to this. If I read a string from string.xml with Swedish special letters it in not a problem. I am using Eclipse 3.6.2 and tha latest version of Android SDK – Christer Sep 11 '11 at 11:53
  • updated with another suggestion, BufferedReader should take care of encoding. – TheCodeKing Sep 11 '11 at 12:32
  • Sorry to say. I still don´t get the right charcters. I also tried onother sugestion, see above. Seems that I have to emigrate:-) – Christer Sep 12 '11 at 04:09
  • I tried an example from my Java book and discovered that a textfile come out properly to the Eclipse Console but not when it was write back to the same file! – Christer Sep 12 '11 at 06:07
  • Thank you! Ihave now discovered that the problem was the source file. When I tested it I used the editor in Eclips and (I think Notpad++) to save the text file. When I saved the same file with OpenOffice Writer coded text - UTF-8. There was no problems. – Christer Sep 18 '11 at 07:12