14

我能够获得图像的高度和宽度。但是有没有一种方法可以检索存储在手机中的图像的大小(以字节或 kb 或 mb 为单位)?

4

5 回答 5

31

谢谢您的回答。这就是我最终解决的方法:

 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; 

感谢您的时间和答案:)

于 2012-02-20T21:43:17.787 回答
11

您只需要创建一个新File对象,例如...

String filepath = Environment.getExternalStorageDirectory() + "/file.png";
File file = new File(filepath);
long length = file.length();
于 2012-02-16T18:40:54.237 回答
9

假设您谈论的是位图(而不是 ImageView),则有一个方法Bitmap.getByteCount()

于 2012-02-16T18:39:03.033 回答
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");
于 2015-09-02T06:54:34.500 回答
2

当您通过右键单击查看文件详细信息时,这将返回与计算机匹配的真实大小。

File file = new File("/sdcard/my_video.mp4");
long length = file.length() / 1024; // Size in KB
于 2014-12-15T21:59:58.340 回答