Я использую ByteArrayOutputStream для помещения текста в текстовое представление из IputStream.
Это отлично работает, но ...
Я из Швеции, и когда я добавляю текст с некоторыми специальными шведскими буквами, он ставит? вместо фактического письма. Система не имеет проблем с этими буквами в противном случае.
Надеюсь, что кто-то там может дать мне подсказку о том, что делать.
Возможно я покажу код:
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();
}
}
Я тоже это связал, получаю с форума (Зельцер):
Хороший мир, но все еще нет шведских букв в выводе:
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
}
}
}
}