Just a qui开发者_JAVA技巧ck question, how to I get a list of directories inside a specified directory.
for example,
String path = Environment.getExternalStorageDirectory().toString()+"/myApp/";
now I'd need to get the list of directories that are inside the "path" directory.
Thanks!
Something like that (add null checking, exceptions etc..)
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// is directory
}
}
I know this has already been answered but I noticed it's missing another, arguably simpler option. So I'm including it for future reference.
File myDirectory = new File("path to some directory");
File[] directories = myDirectory.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
This code saves you having to use a for
loop.
Java 8:
File[] directories = myDirectory.listFiles(File::isDirectory);
File[] files = new File(path).listFiles();
for ( File aFile : files ) {
if ( aFile.isDirectory() ) {
// so on
}
}
Android File API reference here
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);
( Sorry, I couldn't test...) (it must import "android.app.", "android.os.", "android.widget.", "android.view.", "android.view.View.", "java.io."
This example will dir list folder and add to list then display as toast. You need to add permission Read External File, if not, your app will crash when trying to 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();}
}
精彩评论