Is there any way to get the storage size of a Bitmap?

青春壹個敷衍的年華 提交于 2020-01-05 06:38:31

问题


In my app I am downloading some images and I want to know if there is any way I can get the size of a Bitmap that I have downloaded. It's a simple question but i can't seem to find the solution through Google.

here's the code for downloading:

Bitmap b = BitmapFactory.decodeStream((InputStream) new URL(theImageUrl).getContent());

回答1:


First convert the Bitmap into byte[] then you can get the size of bitmap

Try with thye following code

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



回答2:


File file = new File("infilename");

// Get the number of bytes in the file
long length = file.length();



回答3:


Use b.getByteCount(); or do you want to query the size form the server before you download?

EDIT: This method is only available for API Level 12.



来源:https://stackoverflow.com/questions/6424410/is-there-any-way-to-get-the-storage-size-of-a-bitmap

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