在Android中将所有文件读入列表的最有效方法
我的Android应用程序需要查看默认的摄像头目录,并获取在一定时间范围内创建的JPEG文件的名称。我目前使用下面的代码片段来完成这项工作。 其思想是不仅通过“jpg”扩展名过滤掉文件,还使用文件名中包含日期/时间这一事实 我在线程中运行下面的getImages()函数,这样UI本身就不会受到影响。 我注意到,即使在所有这些之后,如果目录中有很多文件,也需要一段时间。有没有更快的算法或Android特定的功能可以让我进一步优化在Android中将所有文件读入列表的最有效方法,android,file,Android,File,我的Android应用程序需要查看默认的摄像头目录,并获取在一定时间范围内创建的JPEG文件的名称。我目前使用下面的代码片段来完成这项工作。 其思想是不仅通过“jpg”扩展名过滤掉文件,还使用文件名中包含日期/时间这一事实 我在线程中运行下面的getImages()函数,这样UI本身就不会受到影响。 我注意到,即使在所有这些之后,如果目录中有很多文件,也需要一段时间。有没有更快的算法或Android特定的功能可以让我进一步优化 private boolean getImages() {
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;
}
你看过Android文件浏览器的例子了吗?也许您可以使用此示例,并根据您的具体需要对其进行调整。我不确定这是否更快,但这是一个很好的起点来考虑另一种方法来达到你的目标。 < P>见
公共类FileUtils{
公共文件[]列表文件阵列(文件目录,文件名筛选器[]筛选器,
int递归){
集合文件=列表文件(目录、筛选器、递归);
File[]arr=新文件[files.size()];
返回文件。toArray(arr);
}
公共集合列表文件(文件目录,
FilenameFilter[]过滤器,int递归){
向量文件=新向量();
File[]entries=directory.listFiles();
if(条目!=null){
用于(文件条目:条目){
用于(FilenameFilter filefilter:filter){
if(filter==null
||文件过滤器
.accept(目录,条目.getName()){
文件。添加(条目);
Log.v(“FileUtils”,“添加:”
+entry.getName());
}
}
if((递归0&&entry.isDirectory()){
递归--;
addAll(列表文件(条目、过滤器、递归));
递归++;
}
}
}
归还文件;
}
}我会看一看,但是一个完整的文件浏览器应用程序会有点杀伤力过大。让我看看那里有没有什么把戏。
class JpegFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.startsWith(captureJPEG.filterString) && name.endsWith(".jpg"));
}
}
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;
}