android,如何获取目录列表?

时间:2011-08-09 14:00:29

标签: java android

一个简单的问题,如何获取指定目录中的目录列表。

例如,

String path = Environment.getExternalStorageDirectory().toString()+"/myApp/";

现在我需要获取“path”目录中的目录列表。

谢谢!

5 个答案:

答案 0 :(得分:39)

类似的东西(添加空检查,例外等......)

File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
    if (inFile.isDirectory()) {
        // is directory
    }
}

答案 1 :(得分:13)

我知道这已经得到了解答,但我注意到它错过了另一个,可以说是更简单的选择。所以我将它包括在内以供将来参考。

File myDirectory = new File("path to some directory");
File[] directories = myDirectory.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.isDirectory();
    }
});

此代码使您无需使用for循环。

答案 2 :(得分:5)

File[] files = new File(path).listFiles();
for ( File aFile : files ) 
     if ( aFile.isDirectory() ) {
          // so on

Android文件API参考here

答案 3 :(得分:1)

    LinearLayout root = new LinearLayout(this);
    File file = new File(Environment.getExternalStorageDirectory().getPath());
    if(file.isDirectory() == false)
    {
        Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();
        return;
    }
    File[] files = file.listFiles();
    int i = 1;
    for(File f : files)
    {
        if(f.isFile() || f.isDirectory())
        {
            try
            {
                LinearLayout layout = new LinearLayout(this);
                layout.setId(i);
                    Button text = new Button(this);
                    text.setText(f.getName());
                    text.setMinWidth(400);
                layout.addView(text);
                root.addView(layout);
                i++;
            }
            catch(Exception e){}
        }
    }
    LinearLayout layout = new LinearLayout(this);
    HorizontalScrollView scroll = new HorizontalScrollView(this);
    scroll.addView(root);
    layout.addView(scroll);
    setContentView(layout);

(抱歉,我无法测试...) (它必须导入" android.app。"," android.os。"," android.widget。&# 34;," android.view。"," android.view.View。"," java.io。 "

答案 4 :(得分:0)

此示例将dir list文件夹添加到列表然后显示为toast。您需要添加权限读取外部文件,如果没有,您的应用程序将在尝试dir“/ sdcard”时崩溃。     

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.TestDirFolder" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:resizeableActivity = "true">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java:

import android.app.*;
import android.os.*;
import android.widget.*;
import java.io.*;
import java.util.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sigL(DirFolder("/")+""); /// <<< HERE!!!
    sigL(DirFolder("/sdcard")+""); /// <<< HERE!!!
    }

    public static ArrayList<String> DirFolder(String path){
        ArrayList<String> ADir=new ArrayList<String>();  
        for (File inFile : new File(path).listFiles()) {
            if (inFile.isDirectory()) {
                ADir.add(""+inFile);
            }} return ADir;
    }

    public void sigL(String s){Toast.makeText(this , s, Toast.LENGTH_LONG).show();}

}