15

I'm able to get the height and width of an image. But is there a method to retrieve the size (in bytes or kb or mb) for an image stored in the phone?

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Ahmed Faisal
  • 4,397
  • 12
  • 45
  • 74

5 Answers5

33

Thank you for your answers. This is how I finally resolved it:

 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
            R.drawable.ic_launcher);


     Bitmap bitmap = bitmapOrg;
     ByteArrayOutputStream stream = new ByteArrayOutputStream();   
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
     byte[] imageInByte = stream.toByteArray(); 
     long lengthbmp = imageInByte.length; 

Appreciate your time and answers :)

Ahmed Faisal
  • 4,397
  • 12
  • 45
  • 74
11

You just need to create a new File object such as...

String filepath = Environment.getExternalStorageDirectory() + "/file.png";
File file = new File(filepath);
long length = file.length();
DRiFTy
  • 11,269
  • 11
  • 61
  • 77
10

Assuming your talking about a bitmap (and NOT an ImageView), there is a method Bitmap.getByteCount().

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • @mehmet Yes, but as of two years ago, when this was posted, it worked. I'll update when I get a chance. – Mike D May 03 '14 at 21:54
  • 1
    use [android.support.v4.graphics.BitmapCompat#getAllocationByteCount](https://developer.android.com/reference/android/support/v4/graphics/BitmapCompat.html#getAllocationByteCount%28android.graphics.Bitmap%29) for compatibility. – Avinash R Aug 27 '15 at 12:01
7
 File file = new File("/sdcard/imageName.jpeg");
 long length = file.length();
 length = length/1024;
 System.out.println("File Path : " + file.getPath() + ", File size : " + length +" KB");
Akshay
  • 6,029
  • 7
  • 40
  • 59
3

This returns the true size that is match in computers when you see file details with right click.

File file = new File("/sdcard/my_video.mp4");
long length = file.length() / 1024; // Size in KB
Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41