Pages

Friday, November 1, 2013

Get Files list from assets folder and sub folder files in android

Hello,
In this posting How to get files list from assets folder and sub folder.




It'a show files to array of String.

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReadAssetsFolderActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_assetsfolder);

  TextView txt = (TextView) findViewById(R.id.textView1);
  //here write folder name which one added assets folder
  
  String[] myfilesfolderlist = listAssetFiles("Myfiles");  
  
  txt.append("Myfiles/Subfolder");
  txt.append("\n");
  
  for (int i = 0; i < myfilesfolderlist.length; i++) {
   txt.append(myfilesfolderlist[i]);
   txt.append("\n");
  }
  String[] mysubfolderlist = listAssetFiles("Myfiles/Subfolder");
  txt.append("\n");
  txt.append("Myfiles/Subfolder");
  txt.append("\n");
  txt.append("\n");
  
  for (int i = 0; i < mysubfolderlist.length; i++) {
   txt.append(mysubfolderlist[i]);
   txt.append("\n");
  }
 }

 //Method for get files from Assets Folder and sub folder  
 private String [] listAssetFiles(String path)  
 {  
  String [] list;  
  try  
  {  
   list = ReadAssetsFolderActivity.this.getAssets().list(path);  
   if (list.length > 0)  
   {  
    return list;  
   }  
  }catch (IOException e)  
  {  
  }  
  return null;  
 }  

}


Output : 



Thank you :)

7 comments:

  1. Replies
    1. Path is name of the folder which one added folder in assets.

      Delete
  2. can you please post full code for download ? thanks

    ReplyDelete
  3. how to show these file using arrayadapter

    ReplyDelete
    Replies
    1. Hello Mehsam saeed,

      You use like below.

      String[] myfilesfolderlist = listAssetFiles("Myfiles");

      pass this String Array to ArrayAdapter.

      ArrayAdapter arrayAdapter = new ArrayAdapter(
      this, android.R.layout.simple_list_item_1, myfilesfolderlist
      );

      thanks

      Delete