gpt4 book ai didi

android - 在 Android 中将所有文件读入列表的最有效方法

转载 作者:行者123 更新时间:2023-11-29 02:14:46 24 4
gpt4 key购买 nike

我的 Android 应用程序需要查看默认的相机目录并获取在某个时间范围内创建的 JPEG 文件的名称。我目前使用以下代码 fragment 执行此操作。这个想法是不仅通过“jpg”扩展名过滤掉文件,而且利用文件名中包含日期/时间这一事实。

我在线程中运行下面的 getImages() 函数,因此 UI 本身不受影响。我注意到即使在这之后目录中有很多文件也需要一段时间。是否有更快的算法或 Android 特定功能可用于进一步优化它?


private boolean getImages()
{
boolean status=true;
File dir = new File(picPath);
//Optimize file search by setting the common file prefix as a filter
//get the date format used by camera to store files.
String startStr=startTime.format3339(false);
String endStr=endTime.format3339(false);
//Loop through each character of start time
//and compare with end time till a mismatch is found
int len=startStr.length();
int idx=0;
for ( idx=0;id < len;idx++ )
{
if (startStr.charAt(idx) != endStr.charAt(idx))
{
Log.d(TAG,"breaking at idx "+ idx);
break;
}
}
filterString=endStr.substring(0,idx).replace("T", " ").replace(":",".");
Log.d(TAG,"Filter String" + filterString);

String[] children = dir.list(new JpegFilter());
List matchedFiles = new ArrayList();
Log.d(TAG,dir.getAbsolutePath()+" has "+ children.length+ " files");
int numfiles = children.length;
for(int i=0;i < numfiles;i++)
{
//Get file modify time
File file = new File(picPath+File.separator+children[i]);
// Get the last modification information.
long lastModified = file.lastModified();
if (lastModified > =startTime.toMillis(false)) //If file modified date greater than equals start time
{
Log.d(TAG,"Match! " + children[i]);
if (lastModified < =endTime.toMillis(false))//If file modified date less than equals end time
{
matchedFiles.add(picPath+File.separator+children[i]);
}
else //We are now over the time range, quit loop to save time
{
break; //TODO break out of for loop
}
}
}
if (!matchedFiles.isEmpty()) //If there are images to be processed
{
status=convertToPDF(matchedFiles,pdfFile);
Log.d(TAG,"convertToPDF() returned " + status);
}

return status;
}

这是我使用的过滤器类


class JpegFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.startsWith(captureJPEG.filterString) && name.endsWith(".jpg"));
}
}

最佳答案

参见 http://androidworkz.com/2010/07/08/source-code-find-files-by-type-play-media-file/

public class FileUtils {

public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
int recurse) {
Collection<File> files = listFiles(directory, filter, recurse);

File[] arr = new File[files.size()];
return files.toArray(arr);
}

public Collection<File> listFiles(File directory,
FilenameFilter[] filter, int recurse) {

Vector<File> files = new Vector<File>();

File[] entries = directory.listFiles();

if (entries != null) {
for (File entry : entries) {
for (FilenameFilter filefilter : filter) {
if (filter == null
|| filefilter
.accept(directory, entry.getName())) {
files.add(entry);
Log.v("FileUtils", "Added: "
+ entry.getName());
}
}
if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
recurse--;
files.addAll(listFiles(entry, filter, recurse));
recurse++;
}
}
}
return files;
}

关于android - 在 Android 中将所有文件读入列表的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4920535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com