私はAndroidが初めてで、検索機能を含むファイルエクスプローラーを開発しようとしています。いくつかのサブフォルダーとファイルを含むフォルダーでは正常に機能する再帰検索機能を使用していますが、何らかの理由で非常に遅く、十分なメモリがないため、多くのサブフォルダーとファイルを含むフォルダーで「強制的に閉じる」ことができます。結果が配置される ArrayList を作成し、リストを埋める再帰関数を呼び出して検索を行います。"path" 引数は検索を開始するファイル、"query" は検索クエリです。
ArrayList<File> result = new ArrayList<File>();
fileSearch(path, query, result);
再帰関数は次のようになります。
private void fileSearch(File dir, String query, ArrayList<File> res) {
if (dir.getName().toLowerCase().contains(query.toLowerCase()))
res.add(dir);
if (dir.isDirectory() && !dir.isHidden()) {
if (dir.list() != null) {
for (File item : dir.listFiles()) {
fileSearch(item, query, res);
}
}
}
}
誰かがより高速かつ/またはより効率的なファイル検索を実行する方法を教えていただければ、本当に感謝しています。
編集:
これは私がAsyncTaskで仕事をしようとした方法です:
private class Search extends AsyncTask<File, Integer, Void> {
String query;
ArrayList<File> result = new ArrayList<File>();
public Search(String query){
this.query = query;
setTitle("Searching");
}
@Override
protected Void doInBackground(File... item) {
int count = item.length;
for (int i = 0; i < count; i++) {
fileSearch(item[i], query, result);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
setProgress(progress[0]);
}
protected void onPostExecute() {
searchResults = new ListItemDetails[result.size()];
for (int i = 0; i < result.size(); i++) {
File temp = result.get(i);
if (temp.isDirectory())
searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
R.drawable.folder, temp.lastModified(), temp.length());
else {
String ext;
if (temp.getName().lastIndexOf('.') == -1)
ext = "";
else
ext = temp.getName().substring(
temp.getName().lastIndexOf('.'));
searchResults[i] = new ListItemDetails(temp.getAbsolutePath(),
getIcon(ext), temp.lastModified(), temp.length());
}
}
finishSearch();
}
}
public void finishSearch() {
Intent intent = new Intent(this, SearchResults.class);
startActivity(intent);
}
finishSearch() の呼び出しは、他のアクティビティで結果を表示するインテントを作成できるようにするためのものです。アイデア、提案、ヒントはありますか?前もって感謝します