How to get image size in Android?

I can get the height and width of the image. But is there a way to get the size (in bytes or kb or mb) for the image stored on the phone?

+4
source share
5 answers

Thank you for your responses. Here is how I finally resolved this:

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 :)

+10
source

Assuming you're talking about a bitmap (and NOT ImageView), there is a Bitmap.getByteCount () method.

+8
source

You just need to create a new File object, for example ...

 String filepath = Environment.getExternalStorageDirectory() + "/file.png"; File file = new File(filepath); long length = file.length(); 
+6
source
  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"); 
+4
source

This returns the true size that matches on computers when you see the file data with a right-click.

 File file = new File("/sdcard/my_video.mp4"); long length = file.length() / 1024; // Size in KB 
+1
source

Source: https://habr.com/ru/post/1396901/


All Articles