我有项目,我不知道如何获取文件夹中的所有文件名。我在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();
}
}
}
答案 0 :(得分:2)
我们假设您要收集storage/emulated/0/DCIM
和&#34; DCIM&#34;中的所有文件。进一步有两个目录让我们说Screenshots
和pictures
。这就是你要做的。
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();
}
};
这样就必须将文件夹名称传递给函数才能获取files
object中的所有文件。希望这有帮助
答案 1 :(得分:1)
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
//do something
}
path
应该是这样的:
的 /存储/模拟/ 0 / Android设备/数据强>
答案 2 :(得分:1)
这可能有效:
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;
答案 3 :(得分:0)
Mehran Zamani的答案可行,但请记得检查文件类型。 请记住,不要只检查扩展名,例如&#34; abc.txt&#34;。 abc文件可能是一个PDF文件,但有人通过简单地重命名它来改变它的扩展名。 出于安全考虑,您必须检查其mime类型。例如,在Java 7中,您现在可以使用Files.probeContentType(path)。这只是一个示例,您可能需要在线搜索更好的解决方案。试着给你一个想法
您可能需要检查它是否是另一个目录。 例如file.isFile()/ file.isFolder()。