How to calculate file size in Android

I am looking to find file size in Android. Initially, I expected it to be as simple as the size () method in a file, or something like that. But I could not find anything. Any help would be greatly appreciated.

thanks

+4
source share
3 answers

Use file.length()

http://download.oracle.com/javase/6/docs/api/java/io/File.html#length%28%29

The length in bytes of the file is indicated by this abstract empty name or 0L if the file does not exist. Some operating systems may return 0L for paths, indicating system-dependent ones such as devices or pipes.

+9
source

File.length() . You were close.

+3
source

This is another solution.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (REQUEST_GET_IMAGE == requestCode && resultCode == Activity.RESULT_OK && data != null) { Uri uri = data.getData(); try { ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r"); Log.i("file size", String.valueOf(parcelFileDescriptor.getStatSize())); } catch (Exception e) { //escape logic here } } } 
+1
source

All Articles