2

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());
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
NotACleverMan
  • 12,107
  • 12
  • 47
  • 67

3 Answers3

3

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;
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
2
File file = new File("infilename");

// Get the number of bytes in the file
long length = file.length();
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
  • Thanks, but I'm not using the File class. I'm using the Bitmap class. Are you recommending casting the bitmap to a File and then doing this? – NotACleverMan Jun 21 '11 at 11:30
  • Why do you need to know how I'm downloading it? I'm just using a Bitmap class. You know: "Bitmap b = new Bitmap();" – NotACleverMan Jun 21 '11 at 11:33
  • As you said that you are downloading a file then the size can be known in downloading procedure there is no way you can use Bitmap to get size or you have to use above method, and dont fear I am not going to steal your code :P – ingsaurabh Jun 21 '11 at 11:36
  • I was just afraid of getting away form the problem. I ahev put ther code there now. Thanks. – NotACleverMan Jun 21 '11 at 11:38
  • sorry @NotACleverMan i was not here but i think you got your answer :D – PedroAGSantos Jun 21 '11 at 13:56
1

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.

Astronaut
  • 6,691
  • 18
  • 61
  • 99