私のAndroidアプリケーションは、デフォルトのカメラディレクトリを調べて、時間範囲内に作成されたJPEGファイルの名前を取得する必要があります。私は現在、以下のコードスニペットでこれを行います。 「jpg」拡張子だけでなく、ファイル名に日付/時刻が含まれているという事実を使用することも、ファイルをフィルタリングすることです。すべてのファイルをAndroidのリストに読み込む最も効率的な方法
スレッド内で以下の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"));
}
}
を参照してください。私はそれを見てますが、全体のファイルブラウザアプリはキル以上のビットになります。私がそこに見つけることができるトリックがあるかどうか見てみましょう。 – SidJ