I have project and I don't know how to get all file names in folder. I work in Andoid Studio. I just need to make function getAllFilesInFolder(string folderPath). I'd be happy if you write all the imports that it needs too. Code I use:
我有项目,我不知道如何获取文件夹中的所有文件名。我在Andoid Studio工作。我只需要使函数getAllFilesInFolder(string folderPath)。如果你写下它所需的所有进口产品,我会很高兴的。我使用的代码:
package com.my.appPackage;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] files = getAllFilesInFolder("/sdcard/folderIUse");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.LargeTextInList, files);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
list.setOnItemClickListener(new ItemList());
}
class ItemList implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
ViewGroup vg = (ViewGroup) view;
TextView tv = (TextView) vg.findViewById(R.id.LargeTextInList);
Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
4 个解决方案
#1
1
This might work:
这可能有效:
ArrayList<String> result = new ArrayList<String>(); //ArrayList cause you don't know how many files there is
File folder = new File("PATH/TO/YOUR/FOLDER/AS/STRING"); //This is just to cast to a File type since you pass it as a String
File[] filesInFolder = folder.listFiles(); // This returns all the folders and files in your path
for (File file : filesInFolder) { //For each of the entries do:
if (!file.isDirectory()) { //check that it's not a dir
result.add(new String(file.getName())); //push the filename as a string
}
}
return result;
#2
2
Let's say you want to gather all the files in storage/emulated/0/DCIM
and "DCIM" futher has two directories let's say Screenshots
and pictures
.This is how you would do it .
假设你想要收集存储/模拟/ 0 / DCIM和“DCIM”中的所有文件,还有两个目录让我们说截图和图片。这就是你要做的。
public void getAllFiles(File parent) {
List<File> directories = new ArrayList<>();
List<File> files = new ArrayList<>();
//adding initial parent whose files we want to fetch
directories.add(parent);
while (directories.size() != 0) {
File f = directories.remove(0);
if (f.isDirectory()) {
if (f.list().length > 0) {
//directory filter to filter out directories if any
List<File> directoryList = Arrays.asList(f.listFiles(directoryFilter));
//file filter to filter out files if any
List<File> fileList = Arrays.asList(f.listFiles(fileFilter));
//adding directories to directory list
directories.addAll(directoryList);
//adding files to file list
files.addAll(fileList);
}
}
}
}
FilenameFilter fileFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return file.isFile();
}
};
FilenameFilter directoryFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
};
This way one has to just pass the folder name to the function to get all the files in files
object.Hope this helps
这样就必须将文件夹名称传递给函数以获取文件对象中的所有文件。希望这有帮助
#3
0
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
//do something
}
path
should be something like this: /storage/emulated/0/Android/data
路径应该是这样的:/ storage / emulated / 0 / Android / data
#4
0
The answer from Mehran Zamani will work but remember to check the file type. Remember don't just check for the extension only like "abc.txt". The abc file maybe is a PDF file but somebody changed its extension by simply rename it. You have to check its mime type for safety purpose. For example in Java 7 you can now just use Files.probeContentType(path). This is just an example, you might need to search for a better solution online. Just trying to give you the idea
Mehran Zamani的答案可行,但请记得检查文件类型。请记住,不要仅像“abc.txt”那样检查扩展名。 abc文件可能是一个PDF文件,但有人通过简单地重命名它来改变它的扩展名。出于安全考虑,您必须检查其mime类型。例如,在Java 7中,您现在可以使用Files.probeContentType(path)。这只是一个示例,您可能需要在线搜索更好的解决方案。试着给你一个想法
You might need to check whether if it is another directory. for example file.isFile() / file.isFolder().
您可能需要检查它是否是另一个目录。例如file.isFile()/ file.isFolder()。
#1
1
This might work:
这可能有效:
ArrayList<String> result = new ArrayList<String>(); //ArrayList cause you don't know how many files there is
File folder = new File("PATH/TO/YOUR/FOLDER/AS/STRING"); //This is just to cast to a File type since you pass it as a String
File[] filesInFolder = folder.listFiles(); // This returns all the folders and files in your path
for (File file : filesInFolder) { //For each of the entries do:
if (!file.isDirectory()) { //check that it's not a dir
result.add(new String(file.getName())); //push the filename as a string
}
}
return result;
#2
2
Let's say you want to gather all the files in storage/emulated/0/DCIM
and "DCIM" futher has two directories let's say Screenshots
and pictures
.This is how you would do it .
假设你想要收集存储/模拟/ 0 / DCIM和“DCIM”中的所有文件,还有两个目录让我们说截图和图片。这就是你要做的。
public void getAllFiles(File parent) {
List<File> directories = new ArrayList<>();
List<File> files = new ArrayList<>();
//adding initial parent whose files we want to fetch
directories.add(parent);
while (directories.size() != 0) {
File f = directories.remove(0);
if (f.isDirectory()) {
if (f.list().length > 0) {
//directory filter to filter out directories if any
List<File> directoryList = Arrays.asList(f.listFiles(directoryFilter));
//file filter to filter out files if any
List<File> fileList = Arrays.asList(f.listFiles(fileFilter));
//adding directories to directory list
directories.addAll(directoryList);
//adding files to file list
files.addAll(fileList);
}
}
}
}
FilenameFilter fileFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return file.isFile();
}
};
FilenameFilter directoryFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
};
This way one has to just pass the folder name to the function to get all the files in files
object.Hope this helps
这样就必须将文件夹名称传递给函数以获取文件对象中的所有文件。希望这有帮助
#3
0
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
//do something
}
path
should be something like this: /storage/emulated/0/Android/data
路径应该是这样的:/ storage / emulated / 0 / Android / data
#4
0
The answer from Mehran Zamani will work but remember to check the file type. Remember don't just check for the extension only like "abc.txt". The abc file maybe is a PDF file but somebody changed its extension by simply rename it. You have to check its mime type for safety purpose. For example in Java 7 you can now just use Files.probeContentType(path). This is just an example, you might need to search for a better solution online. Just trying to give you the idea
Mehran Zamani的答案可行,但请记得检查文件类型。请记住,不要仅像“abc.txt”那样检查扩展名。 abc文件可能是一个PDF文件,但有人通过简单地重命名它来改变它的扩展名。出于安全考虑,您必须检查其mime类型。例如,在Java 7中,您现在可以使用Files.probeContentType(path)。这只是一个示例,您可能需要在线搜索更好的解决方案。试着给你一个想法
You might need to check whether if it is another directory. for example file.isFile() / file.isFolder().
您可能需要检查它是否是另一个目录。例如file.isFile()/ file.isFolder()。