2016-09-29 1 views
0

사용자의 내부 저장소에 MP3 파일이 들어있는 모든 폴더의 목록을 가져 오려고합니다. 하지만의 지연, 내 장치에서이 코드를 실행하면파일 저장 액세스 속도를 높이려면 어떻게해야합니까?

public void listAllMusicFiles(String pathToDirectory) { 
     String lastFolderPath = ""; 
     int mp3Count = 0; 
     File f = new File(pathToDirectory); 
     File[] files = f.listFiles(); 
     for (File inFile : files) { 
      if (inFile.isDirectory()) { 
       //reset last folder path 
       lastFolderPath = ""; 
       Log.d("Directory ", inFile.getPath()); 
       listAllMusicFiles(inFile.getPath()); 
      } else { 
       if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) { 
        mp3Count++; 
        Log.wtf("MP3 Count", mp3Count + " "); 

        //add each folder only once 
        String folderName = inFile.getParentFile().getName(); 
        String folderPath = inFile.getParentFile().getPath(); 
        Log.e("FOUND in", folderPath); 

        //create a new Folder object 
        Folder currentFolder = new Folder(folderName, folderPath, mp3Count + ""); 

        if (!lastFolderPath.equals(folderPath)) { 
         Log.d("NEW", folderPath); 
         lastFolderPath = folderPath; 
         folderArrayList.add(currentFolder); 
        } else { 
         Log.d("OLD", folderPath); 
         //find a Folder object in folderArrayList where the object's path matches current folderPath 
         for (Folder folder : folderArrayList) { 
          String currentPath = folder.getFolder_Path(); 
          if (currentPath.equals(folderPath)) { 
           //found a match 
           //update count 
           folder.setFolder_Song_Count(mp3Count + ""); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

, 나는이 RecyclerView에 필요한 폴더를 나열 할 수 있어요 - 여기

나는이 목적을 위해 호출하고있는 재귀 함수입니다 약 6-7 초.

이 작업을 이미 AsyncTask로 옮겼습니다.이 집중적 인 작업으로 인해 UIThread가 중단되지 않습니다.

그러나 파일 시스템 성능을 향상시키는 데는 완전히 손실됩니다. 친절하게 도와주세요. 감사 !

+0

'listAllMusicFiles (문자열 pathToDirectory)'처럼 HashMap에 사용할 수 있습니다. 스팅과 파일간에 뒤쪽과 요새를 변환 중입니다. 그것을 'listAllMusicFiles (파일 디렉토리)'로 만드십시오. – greenapps

+0

'folderName = inFile.getParentFile(). getName();' 그것은 'f.getName();'입니다. – greenapps

+0

디렉터리에 100 개의 mp3 파일이있는 경우 100 번 반복하여 배열 목록을 반복하여 해당 폴더가 이미 있는지 확인하십시오. 그런 다음 해당 폴더의 수를 백 번 변경합니다. 그것은 효율적 일 수 없습니다. 먼저 해당 폴더의 파일을 계산해야합니다. 그리고 그 폴더로 할 때 이름을 추가하고 한 번 배열에 계산하십시오. 당신은 그 때 무엇이든을 검사해야하지 않는다. – greenapps

답변

0

대신 해당 폴더를 찾기 위해 ArrayList에와 전체 목록을 반복 다음 단계에서 currentFolder를 저장하고 값을 업데이트하는, 당신은 단순히이

HashMap<String, Folder> folders = new HashMap<>(); 

    public void listAllMusicFiles(String pathToDirectory) { 

     int mp3Count = 0; 
     File f = new File(pathToDirectory); 
     File[] files = f.listFiles(); 

     Folder folder; 
     String folderName, folderPath; 

     for (File inFile : files) { 
      if (inFile.isDirectory()) { 
       //reset last folder path 
       Log.d("Directory ", inFile.getPath()); 
       listAllMusicFiles(inFile.getPath()); 
      } else { 
       if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) { 
        mp3Count++; 
        Log.wtf("MP3 Count", mp3Count + " "); 

        //add each folder only once 
        folderName = inFile.getParentFile().getName(); 
        folderPath = inFile.getParentFile().getPath(); 

        Log.e("FOUND in", folderPath); 

        if (folders.containsKey(folderPath)) { 

         folder = folders.get(folderPath); 
         folder.setFolder_Song_Count(mp3Count + ""); 
         folders.put(folderPath, folder); 
        } else { 

         folder = new Folder(folderName, folderPath, mp3Count + ""); 
         folders.put(folderPath, folder); 
        } 

       } 
      } 
     } 
    } 
+0

은 멀티미디어 사용에 가장 적합합니다. MediaStore. . –

관련 문제