NotACleverMan
NotACleverMan

Reputation: 12237

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

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

Upvotes: 2

Views: 2747

Answers (3)

Astronaut
Astronaut

Reputation: 7051

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.

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53687

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;

Upvotes: 3

PedroAGSantos
PedroAGSantos

Reputation: 2346

File file = new File("infilename");

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

Upvotes: 2

Related Questions