0

I'm downloading images from the internet and then displaying them scaled on the screen. The idea is to use inSampleSize so I can scale large images while decompressing and prevent OutOfMemory exception. But in order to find inSampleSize I need to know image resolution. It can be obtained using inJustDecodeBounds option.

The problem is that I can't pass it the stream and download the image from the internet directly, because it will be downloaded twice (first to get the size, then to get the scaled Bitmap). I can't download the image and store it in RAM, because image size may be large. The only solution left is to download the image to SD card / internal memory and read it from there, but when user has no space left bad things are going to happen.

The question is - is there any way to do it without relying on the storage and RAM memory which doesn't require the image to be downloaded twice? Or maybe BitmapFactory doesn't download whole image when it's using inJustDecodeBounds, but just headers?

Thanks

Sebastian Nowak
  • 5,607
  • 8
  • 67
  • 107
  • Alternative to ram and sd? hmmm... jokes aside think its pretty standard download mechanism, dunno with bitmapfactory but would expect it to behave similar. All in all, I don't get why this ever would be a problem. Can't you just check if there is room for it and if not you can't allow download. – Warpzit Oct 31 '11 at 18:49
  • It's always worth checking whether there's a better way. If it would be possible to get the image size without downloading the whole file such mechanism would be much more reliable. – Sebastian Nowak Oct 31 '11 at 18:53
  • Aggree :) but you would still have memory issues... if having the picture twice is a issue why wouldn't once be? I just can't make sence of the question :) – Warpzit Oct 31 '11 at 18:59
  • Having picture twice isn't an issue, downloading it twice is. – Sebastian Nowak Oct 31 '11 at 19:03
  • Ah now I get your problem ;) hmmm as the first answer suggest reading the header. But I think it will be hard to subtract only the header from the file you want to download (probarbly possible though) – Warpzit Oct 31 '11 at 20:43

1 Answers1

1

If you're downloading a PNG, GIF or JPG it should be possible to read the file headers to determine the width and height, which should mean you avoid having to download the entire file.

This post has some C# code for reading the width and height from the headers for the above format which you could port to Java for use in Android.

Community
  • 1
  • 1
Ian Newson
  • 7,679
  • 2
  • 47
  • 80