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.
Asked
Active
Viewed 58 times
0

bhanu kaushik
- 391
- 1
- 7
- 30
-
what are u doing for this?. u can paste .txt file in assests for try alternatively. – Manmohan Badaya Jan 25 '14 at 06:45
-
SQLite database is better way to achieve this my frnd... :) – Chirag Ghori Jan 25 '14 at 06:46
-
how to do if i pasted it in assets folder ... – bhanu kaushik Jan 25 '14 at 06:47
-
From assests u can easily read the file try a google search for this – Manmohan Badaya Jan 25 '14 at 06:59
-
i tried putting in asset folder first and implementing this code – bhanu kaushik Jan 25 '14 at 07:02
-
AssetManager am = context.getAssets(); InputStream is = am.open("test.txt"); – bhanu kaushik Jan 25 '14 at 07:03
-
@BlackTigersir , i have no idea about the SQLite db ... by the time i am learning SQLite i am trying this – bhanu kaushik Jan 25 '14 at 07:04
1 Answers
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