如何仅将SD卡中的文件夹名称提取到ListView
?
在我的一个教会应用程序中,我允许用户创建一个事件(例如:Phase-I Church - Evening Prayer_Part1),然后创建一个具有相同名称的文件夹
SD Card/Church Application/EventNameFolders
所以现在我的目标是将Church Application中的所有文件夹名称显示为ListView
。
答案 0 :(得分:2)
private ListView lv = (ListView) findViewById(R.id.your_list_view_id);
List<String> your_array_list = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory().toString()+"/Church Application/";
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// in here, you can add directory names into an ArrayList and populate your ListView.
your_array_list.add(inFile.getName());
}
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
删除空文件夹
String path = Environment.getExternalStorageDirectory().toString()+"/Church Application/";
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// If this folder is empty, delete it.
if (inFile.listFiles().length == 0) {
inFile.delete();
}
}
}
删除空文件夹并仅显示非空文件夹
private ListView lv = (ListView) findViewById(R.id.your_list_view_id);
List<String> your_array_list = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory().toString()+"/Church Application/";
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// If this folder is empty, delete it.
if (inFile.listFiles().length == 0) {
inFile.delete();
// If not, add to ArrayList
} else if (inFile.listFiles().length >= 1) {
your_array_list.add(inFile.getName());
}
}
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
答案 1 :(得分:0)
File[] file = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) {
file[] innerFiles = f.listFiles();
for(int i=0; i< innerFiles.length;i++){
Log.i("Name", innerFiles[i].getPath() + "");
}
}
if (f.isFile()) {your code}
}