我能够获取图像的高度和宽度。但是,是否有一种方法可以检索存储在手机中的图像的大小(以字节,KB或MB为单位)?
我能够获取图像的高度和宽度。但是,是否有一种方法可以检索存储在手机中的图像的大小(以字节,KB或MB为单位)?
感谢您的答案。这是我最终解决它的方法:
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;
感谢您的时间和回答 :)
bitmap.getByteCount()
。请记住,“压缩”版本是JPEG格式,因此如果您比较两个大小,则不一定是在比较相同的内容。 - Douglas Kazumi您只需要创建一个新的File
对象,例如...
String filepath = Environment.getExternalStorageDirectory() + "/file.png";
File file = new File(filepath);
long length = file.length();
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");
当您右键单击文件并查看文件详细信息时,此命令将返回与计算机上显示的实际大小相匹配的大小。
File file = new File("/sdcard/my_video.mp4");
long length = file.length() / 1024; // Size in KB