0

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.

Vinod
  • 88
  • 3
  • 8

1 Answers1

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
  • 9
    The 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