如何在 Android Textview 中显示像 ë 和 ª 这样的字符?

How to display characters like ë and ê in Android Textview?

我的资产文件夹中有一个文本文件,其中有一些德语和中文文本。我使用此代码读取文件:

public String loadJSONFromAsset() {
    String json = null;

    try {
        InputStream is = getActivity().getAssets().open("name.json.txt");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }

    return json;
}

但是在我的TextView中显示时,它显示为菱形内的问号。我的代码有什么问题?有人请帮忙。

试试这个方法:

try {
File fileDir = new File("name.json.txt");

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"));

String str = null;

while ((str = in.readLine()) != null) {
    System.out.println(str);
}

        in.close();
} 
catch (UnsupportedEncodingException e) 
{
    System.out.println(e.getMessage());
} 
catch (IOException e) 
{
    System.out.println(e.getMessage());
}
catch (Exception e)
{
    System.out.println(e.getMessage());
}

这可能是因为您的文件使用不同的 charset 编码,例如使用 MS 时默认使用 ANSI记事本。您可以尝试使用不同的字符集以获得预期的结果。

json = new String(buffer, "ISO8859-1");