2012-10-17 4 views
5

I 응용 프로그램 use.I에 파일을 저장 장치의 내부 메모리에 디렉토리를 만드는 오전에 파일의 수를 확인합니까 디렉토리가 존재하지 않거나 디렉토리가 이미 생성 된 경우 기존 파일 객체를 반환합니다.이 디렉토리에 포함 된 파일의 수와 이름을 확인하고 싶습니다.이 파일 객체를 사용하여이 작업을 어떻게 수행합니까?안드로이드 어떻게 사용하고 디렉토리

참고 : 장치의 외부 메모리를 사용하고 싶지 않습니다. 디렉토리에 파일이 디렉토리에없는 다른 파일과 분리되어 있어야하므로 디렉토리를 만들지 않을 수 없습니다.

업데이트 된 코드 : 사전에

private static boolean IsFoldercontainsFiles(Context context) { 
     // TODO Auto-generated method stub 
     File dir = context.getDir(USER_FAV, Context.MODE_PRIVATE); 
     if (dir.exists()) { 
        if (dir.listFiles() == null){ 
        return false;} //it never execute this line even though its null 
        else{ 
      File childfile[] = dir.listFiles(); 
      Log.i("file no is ", Integer.toString(childfile.length)); 
      if (childfile.length == 0) { 
       return false; 
      } else { 
       return true; 
      } 
       } 
     } else { 
      return false; 
     } 
    } 

감사

답변

8

확인 당신에게 아이의 목록을 제공합니다 FilelistFiles 방법.

+0

이 작업을해야합니다 ... 시도해 보겠습니다. – Anshul

+0

하지만 (dir.listFiles() == null) 다음을 확인하면 항상 sa입니다. y는 그 null이 아니라 비록 thr 디렉토리에 아무것도 없다. 널 확인을위한 잘못된 길을 검사? 제발 도와주세요 – Anshul

+0

코드를 게시 할 수 있습니까? – Rajesh

5

은 "디렉토리"에 대한 모든 childfiles을 반환이

여기
File childfile[] = dir .listFiles(); 
for (File file2 : childfile) { 
    Log.v("file name is ", file2.getName()); 
} 

때 listFiles()를 시도하고 이것을 반복하면

+0

고마워 Sharath ... 당신의 해결책은 정확합니다 – Anshul

+0

하지만 만약 내가 (dir.listFiles() == null) 다음 그것은 항상 null이 아니라고 말할 thr 디렉토리에 아무것도 없다고 확인하십시오. null check에 대한 잘못된 방법을 확인하고 있습니까? – Anshul

+0

코드를 업데이트 할 수 있습니까? –

7
File file=new File("/mnt/sdcard/yourfolder"); 
     File[] list = file.listFiles(); 
     int count = 0; 
     for (File f: list){ 
      String name = f.getName(); 
      if (name.endsWith(".jpg") || name.endsWith(".mp3") || name.endsWith(".some media extention")) 
       count++; 
      System.out.println("170 " + count); 
     } 
-1

간단한 childfiles의 이름을 얻을하는 데 도움이 될 것입니다 :

File dir = new File(path); // "/mnt/sdcard/yourfolder" 

long totalNumFiles = dir.listFiles().length; 
+2

코드 만 덤프하는 것이 아니라 세부 정보를 추가합니다. –

관련 문제