deep Get Assets File List - Android App

Android examples for App:Assets File

Description

deep Get Assets File List

Demo Code

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;

public class Main {

  public static List<String> deepGetAssetsFileList(Context context,
      String rootFile) {/* w w w. j a va  2  s . com*/
    AssetManager mgr = context.getAssets();
    List<String> fileList = new ArrayList<String>();
    deepGetAssetsFileList(mgr, rootFile, fileList);
    return fileList;
  }

  private static void deepGetAssetsFileList(AssetManager mgr, String curFile,
      List<String> fileList) {
    boolean isDir = isDir(mgr, curFile);
    if (isDir) {
      String[] fileListStrArr = new String[0];
      try {
        fileListStrArr = mgr.list(curFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
      for (String subFile : fileListStrArr) {
        if (curFile.length() > 0) {
          subFile = curFile + "/" + subFile;
        }
        deepGetAssetsFileList(mgr, subFile, fileList);
      }
    } else {
      fileList.add(curFile);
    }
  }

  private static boolean isDir(AssetManager mgr, String file) {
    boolean isDir = false;
    InputStream input = null;
    try {
      input = mgr.open(file);
      input.close();
    } catch (Exception e) {
      isDir = true;
    }
    return isDir;
  }
}

Related Tutorials