Determine if BitmapFactory.decodeStream () succeeded without errors

I am downloading bitmaps from the internet using BitmapFactory.decodeStream()

AsyncTask internally. In my application, I have an image list viewer and I only want to download the bitmaps that the user can see and cancel all download requests for the bitmaps that the user has scrolled through. After the download is complete, I cache the bitmap so I don't have to download it again. To cancel the bitmap downloads I call AsyncTask.cancel(true)

.

The problem is that it sometimes BitmapFactory.decodeStream()

throws a warning that an InterruptedIOException event has occurred (since I canceled the task while it was loading), but it won't actually throw an exception. Usually, when this happens, the function returns with a semi-loaded bitmap. Unfortunately, since the returned bitmap is not null, the application simply assumes that the download was successful and caches that half of the downloaded bitmap. From now on, the user will loop over the half-loaded bitmap until the cache is cleared.

So my question is, is there a way to check if the BitmapFactory.decodeStream()

bitmap was actually loaded?

+3


source to share


1 answer


Although I was not able to figure out if it succeeded BitmapFactory.decodeStream()

without errors, I did find a workaround. I am just checking if the task was canceled before the call BitmapFactory.decodeStream()

and after the call BitmapFactory.decodeStream()

to determine if the task was canceled during the call BitmapFactory.decodeStream()

. If so, I just assume BitmapFactory.decodeStream()

it failed (which it probably does) and execute the necessary cleanup code to ensure that the bitmap never gets into the cache.



0


source







All Articles