0

我只是android的初学者。我的应用程序包含显示引号的图像数量(具有相同的背景)。现在我正在做的是将引号保存在 .txt 文件中并放在/res/raw文件夹中。我正在尝试将这些引号从文件显示到活动,但没有得到任何结果。有什么建议么。

4

1 回答 1

0

从文件中读取的方法:

  public byte[] readFileFromRaw(Context context, int fileId) throws IOException {
        Resources res = context.getResources();
        InputStream inputStream = res.openRawResource(fileId);

        int i = 0;
        while (inputStream.read() != -1) {
            i++;
        }

        inputStream.reset();
        byte[] b = new byte[i];
        inputStream.read(b);
        return b;
    }

现在您可以通过以下方式使用它:

byte[] b = readFileFromRaw(this, R.raw.my_quotes);

如果您需要字符串,请执行以下操作:

String quote = new String(b);
于 2014-01-25T06:48:52.760 回答