vjuliano
vjuliano

Reputation: 1503

How to calculate the size of a file in Android

I am looking to find the size of a file in Android. I originally expected it to be as simple as a size() method in File, or something along those lines. But I haven't been able to find anything. Any help would be greatly appreciated.

Thanks

Upvotes: 6

Views: 4294

Answers (3)

Hajime
Hajime

Reputation: 149

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 
    }
  }
}

Upvotes: 2

corsiKa
corsiKa

Reputation: 82559

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 denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.

Upvotes: 11

EboMike
EboMike

Reputation: 77722

File.length(). You were close.

Upvotes: 3

Related Questions