Successfully i wrote program to read single file in asset folder and assign it to text view. Now i want to read all files and assign it to the text view, will any one of you help me how to do? all the files are text files, thankful to you in advance.
Asked
Active
Viewed 8,117 times
0
-
2what you are putting in asset folder. – Monty Jan 04 '13 at 09:34
-
wat kind of file u want to read – Venkatesh S Jan 04 '13 at 09:34
-
1as you did for single file, now do it for all files. best of luck. – RobinHood Jan 04 '13 at 09:36
-
Try looking at this - http://stackoverflow.com/questions/5771366/reading-a-simple-text-file – jcw Jan 04 '13 at 09:41
1 Answers
1
****In Android we can't read file from assets folder u have to copy file from asseset to sdcard than perform reading**
EDIT: this statement is wrong. See comments.
use following code for perform copy from assets folder
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}

Daniyal Javaid
- 1,426
- 2
- 18
- 32

Yogesh Tatwal
- 2,722
- 20
- 43
-
9The statement _"In Android we can't read file from assets folder"_ is incorrect. Of course you can read a file from the assets folder. We do it all the time. In fact, in the code snippet you provided you are reading files from the assets folder in order to copy them to the SD card! – David Wasser Jan 04 '13 at 10:18
-
We can read but we can not write to asset folder.Please modify your answer. – avck Jan 20 '15 at 06:49