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 :)