我正在开发一个 android 项目,我正在尝试从 SD 卡中获取文件和目录的列表。除了文件名输出一堆废话之外,它似乎更不工作,我不明白为什么。
下面是我用来获取文件列表的代码。
公共 ArrayList getFileDirectoryListing() { ArrayList fileAndDirectories = new ArrayList();
final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File[] files = Environment.getExternalStorageDirectory().listFiles();
for (int i = 0; i < files.length; i++)
{
FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails();
fileDirectoryDetails.path = files[i].getName();
if (files[i].isDirectory())
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.Directory;
}
else
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.File;
}
fileAndDirectories.add(fileDirectoryDetails);
}
}
return fileAndDirectories;
}
下面是我用来设置列表适配器的代码
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listView = getListView();
ArrayList<FileDirectoryDetails> filesAndDirectories = getFileDirectoryListing();
fileDirectoryDetailsArrayAdapter = new
ArrayAdapter<FileDirectoryDetails>(this, android.R.layout.simple_list_item_1, filesAndDirectories);
setListAdapter(fileDirectoryDetailsArrayAdapter);
}
下面是我在列表视图中返回的屏幕截图,而不是实际文件名。