如何查找所有文件在Android设备,并把它们放在一个列表? [英] How to look up for all files in an android device and put them in a list?
问题描述
我寻求帮助,以列出Android的外部存储设备上的所有文件。我想查找在所有文件夹,包括子文件夹主文件夹。有什么办法呢?
我在一个基本的工作,但还没有获得所需的结果。它不工作。
这里是我的code:
文件[] files_array;
files_array =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath())。listFiles()。
请帮忙。谢谢你。
编辑:
该方法返回0的大小。我不知道是什么回事。这是我的活动:
公共类主要扩展ListActivity {
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
清单<文件>文件= getListFiles(Environment.getExternalStorageDirectory());
setListAdapter(新ArrayAdapter&所述;文件>(Main.this,android.R.layout.simple_list_item_1,文件));
Toast.makeText(这一点,+ files.size(),Toast.LENGTH_LONG).show();
} 私人列表<文件> getListFiles(文件parentDir){
//在第一个电话,是parentDir你的SD卡的根路径
ArrayList的<文件> inFiles =新的ArrayList<文件>(); //初始化数组列表来存储文件名
文件[] =文件parentDir.listFiles(); //列表这个目录中的所有文件
对于(文件文件:文件){
如果(file.isDirectory()){//如果该文件是一个目录
inFiles.addAll(getListFiles(文件)); // **调用这个递归以获得所有LOWER级的文件**
} }
返回inFiles;
}
}
我发现在网络上的某个地方的解决方案,我问我自己为什么不分享。
公共无效walkdir(文件目录){ 文件[]的文件列表;
的文件列表= dir.listFiles(); 如果(的文件列表!= NULL){
的for(int i = 0; I< listFile.length;我++){
如果(listfile中[I] .isDirectory()){
walkdir(的文件列表[我]);
}其他{
如果(的文件列表[I] .getName()。与toLowerCase()的endsWith(PDF)){
files_list.add(的文件列表[我]);
}
}
}
}
}
你所需要的其实就是调用上面要开始与父目录的方法是什么。我建议把Environment.getExternalStorageDirectory()中的父目录,这样就不会与不同的设备改变。
I looking for help to list all files in android external storage device. I want to look up in all the folders including the subfolders for the main folder. Is there any way to this?
I have worked on a basic one but still haven't get the desired result. It doesn't work. Here is my code:
File[] files_array;
files_array = new File(Environment.getExternalStorageDirectory().getAbsolutePath()).listFiles();
Please help. Thank you.
Edit:
This method returns 0 size. I don’t know what is the matter. This is my activity:
public class Main extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<File> files = getListFiles(Environment.getExternalStorageDirectory());
setListAdapter(new ArrayAdapter<File>(Main.this, android.R.layout.simple_list_item_1, files));
Toast.makeText(this, "" + files.size(), Toast.LENGTH_LONG).show();
}
private List<File> getListFiles(File parentDir) {
// On first call, parentDir is your sdcard root path
ArrayList<File> inFiles = new ArrayList<File>(); // initialize an array list to store file names
File[] files = parentDir.listFiles(); // list all files in this directory
for (File file : files) {
if (file.isDirectory()) { // if the file is a directory
inFiles.addAll(getListFiles(file)); // **CALL THIS RECURSIVELY TO GET ALL LOWER LEVEL FILES**
}
}
return inFiles;
}
}
I have found a solution somewhere on the web and I asked my self why not share it.
public void walkdir(File dir) {
File[] listFile;
listFile = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().toLowerCase().endsWith(".pdf")){
files_list.add(listFile[i]);
}
}
}
}
}
All what you need actually is calling the method above with the parent dir that you want to start with. I recommend to put Environment.getExternalStorageDirectory() in the parent directory so it will not change with different devices.
这篇关于如何查找所有文件在Android设备,并把它们放在一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!