2014-11-21 5 views
0

그의 경로로 파일 크기를 가져 오려고합니다. 가 나는 다른 모든 정보를 얻을 수 있기 때문에 내가 잘못 어디 모르겠지만, 파일의 크기를 얻을 때, 그것은 내가 다른 모든 정보 권한을 얻을 수 0그의 경로에서 파일 크기 받기

File file = new File(Environment.getExternalStorageDirectory(), MainActivity.filePath); 
long fileLength = file.length(); //here is where i get 0 
String fileSize = String.valueOf(fileLength); 
String fileName = file.getName(); 
String fileExtension = MainActivity.filePath.substring((MainActivity.filePath.lastIndexOf(".") + 1), MainActivity.filePath.length()); 

을 반환 그래서 그것은 경로 문제가 아닙니다.

+0

'file.length()'는 예상대로 작동합니다. 파일이 디렉토리이면 0, 파일이 비어있는 경우, 파일을 읽을 수없는 경우 또는 파일이없는 경우 0을 얻습니다. –

답변

0
File file=new File(path_to_file); 
FileInputStream fin=new FileInputStream(file); 
int size =fin.available(); 
1

파일이 없어도 파일 이름을 얻을 수 있습니다. file.exists()는 무엇을 반환합니까? 나는 거짓을 추측하고있다. 조심스럽게 경로를 확인하십시오.

0

단순히 길이()를 사용하고 KB, MB, GB 등으로 변환 :

파일 파일 = 새로운 파일 (파일 경로를)

if(file.exists()){ 

    double bytes = file.length(); 
    double kilobytes = (bytes/1024); 
    double megabytes = (kilobytes/1024); 
    double gigabytes = (megabytes/1024); 
    double terabytes = (gigabytes/1024); 
    double petabytes = (terabytes/1024); 
    double exabytes = (petabytes/1024); 
    double zettabytes = (exabytes/1024); 
    double yottabytes = (zettabytes/1024); 

    System.out.println("bytes : " + bytes); 
    System.out.println("kilobytes : " + kilobytes); 
    System.out.println("megabytes : " + megabytes); 
    System.out.println("gigabytes : " + gigabytes); 
    System.out.println("terabytes : " + terabytes); 
    System.out.println("petabytes : " + petabytes); 
    System.out.println("exabytes : " + exabytes); 
    System.out.println("zettabytes : " + zettabytes); 
    System.out.println("yottabytes : " + yottabytes); 
}else{ 
    System.out.println("File does not exists!"); 
}