0

私のアンドロイド プロジェクトでは、 sdカードの画像をarrayに保存する必要がありました。フォルダ内のすべての画像をフィルタリングして取得できます。しかし、私が本当にする必要があるのは、すべての画像ではなく特定の画像をフィルタリングして取得することです。私のコードセグメントは、

File[] imagelist = filePath.listFiles(new FilenameFilter(){  
            public boolean accept(File dir, String name)  {  
                return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
            }  
        });

それで、誰かが役に立つコードセグメントを手伝ってくれませんか。ありがとうございました!

4

1 に答える 1

1

わかりました。文字列配列に必要な名前のリストがある場合、フィルターを通過するすべてのファイルに対して、リストをループし、ファイルの名前を配列と比較して、存在するかどうかを確認する必要があります。もしそうなら、それはあなたが望むものです。

File[] imagelist = filePath.listFiles(new FilenameFilter(){
  public boolean accept(File dir, String name){
    if(!(name.endsWith(".jpg") || name.endsWith(".png")) return false; // Only need images
    for(String validName: namesArray){
      // If the names in the list include the file extention then use this line
      if(name.equals(validName)) return true;
      // Otherwise If the names in the list don't include the file extention then use these lines
      if(name.endsWith(".jpg") && name.substring(0, name.lastIndexOf(".jpg")).equals(validName)) return true;
      if(name.endsWith(".png") && name.substring(0, name.lastIndexOf(".png")).equals(validName)) return true;
    }
    return false;
  }  
});
于 2013-10-31T05:49:59.160 に答える