0

I am just a beginner for android. My application consists of number of images displaying quotes(having same background) . Now what i am doing is i have saved the quotes in the .txt file and placed in /res/raw folder . I am trying to display these quotes from the file to the activity but not getting any result. Any suggestions.

bhanu kaushik
  • 391
  • 1
  • 7
  • 30

1 Answers1

0

Method to read from file:

  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;
    }

Now you can use it in next way:

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

If you need string just do that:

String quote = new String(b);
Divers
  • 9,531
  • 7
  • 45
  • 88