How to get the size of an image in Android?

拟墨画扇 提交于 2019-12-03 19:14:57

问题


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?


回答1:


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




回答2:


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();



回答3:


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




回答4:


 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");



回答5:


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


来源:https://stackoverflow.com/questions/9316986/how-to-get-the-size-of-an-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!