如何获取SD卡上所有目录的列表?
答案 0 :(得分:2)
尝试使用以下代码获取目录列表
public static ArrayList<String> getAllDirectoriesFromSDCard(Activity activity) {
ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) {
absolutePathOfImageList.add(f.getAbsolutePath());
}
}
// Log.i(TAG,"........Detected images for Grid....."
// + absolutePathOfImageList);
return absolutePathOfImageList;
}
答案 1 :(得分:1)
通过所有目录我认为你的意思甚至是sdcard中的子目录,如果是的话那就是你需要做什么
1)初始化一个ArrayList
private ArrayList<String> allDirectories = new ArrayList<String>();
2)复制并粘贴此功能:
public void listAllDirectories(String path) {
File tempfile = new File(path);
File[] files = tempfile.listFiles();
if (files != null) {
for (File checkFile : files) {
if (checkFile.isDirectory()) {
allDirectories.add(checkFile.getName());
listAllDirectories(checkFile.getAbsolutePath());
}
}
}
}
3)从任何地方打电话
listAllDirectories(Environment.getExternalStorageDirectory().toString());
for (int i = 0; i < allDirectories.size(); i++) {
System.out.println(allDirectories.get(i));
}
答案 2 :(得分:0)
请尝试以下代码。
代码:
System.out.println(" File List Start" );
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (int i=1;i<=file.length;i++){
System.out.println(" No. "+i+" : "+file[i-1].getAbsolutePath());
}
System.out.println(" File List End" );