6

I am loading an image from a URL to be shown in an ImageView. The standard way is to create a bitmap as follows (where the InputStream is has been obtained from the URL):

Bitmap bm = BitmapFactory.decodeStream(is);

But I am dealing with large images, and I want to start displaying pixels before the entire image is loaded in the bitmap i.e. I want achieve progressive rendering of the image (similar to loading of large images in web browsers). If the image is, for instance, an interlaced PNG, this will allow me to show a lower quality image to users while they wait for the full image to load. Basically, I want to achieve rendering as shown in http://www.codinghorror.com/blog/2005/12/progressive-image-rendering.html

I know if I could implement a PNG decoder, or use an opensource implementation, I could modify it to render pixels to the ImageView's bitmap as soon as I read them, but that looks like a mammoth effort for what I intend.

Anvit Tawar
  • 81
  • 1
  • 7

2 Answers2

0

Simple progressive as-received rendering: This is achieved by considering transmission network layer (TCP). Every packet received in order, is delivered in the application layer which is displayed immediately. If you wish to achieve this, then you need to think about socket programming in you application.

However, this is an old approach and actually more computation intensive one. I personally prefer, working out with a stub image. The stub image is shown at the start, and an immediate thread with a url connection and download (manager or cache) is started. Once the download is completed, you get back to your main thread to update the photo. This is more organizable.

Please correct me if I'm wrong in any sense.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
  • The main issue with your first approach is that the image data does not come in straight pixels, but instead it is compressed and organized according to the image format I use (eg. PNG). Even if I deal with individual packets, the major effort would be to convert that data into pixel color values before rendering, which amounts to writing a decoder for the said image format (this would be an overkill). I was rather looking for an existing method/library which would be simpler. Your second approach, of course, is way easier. I've kept that as my backup option if nothing else works :) – Anvit Tawar Jun 20 '13 at 21:58
0

Try Facebook's Freso project: http://frescolib.org/#streaming I'm not sure what file formats it supports, but it supports progressive rendering of JPEGs that have been encoded progressively.

Adam
  • 43,763
  • 16
  • 104
  • 144