问题
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