I'm downloading images from a url and storing them in a SQLite database table. The images are profile pictures for users. I store the profile image and the user's information in one row. One row per user.
This mostly works. Almost all images load fine into my list. However, I have 2-4 开发者_C百科out of about 300 users that display with a black image. I verified that the users with black images have a valid image at the url by downloading the image via my web browser.
Anybody got an idea why these few users are showing black images? I'm not seeing any exceptions.
Here is my code:
ByteArrayBuffer baf = new ByteArrayBuffer(50);
URL url = new URL(imageURL);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
...
// Then the byte[] is stored in a SQLite blob field.
baf.toByteArray()
...
// Then I load the blob field like this into my view.
byte[] bytes = cursor.getBlob(cursor.getColumnIndex(DbHelper.PROFILE_IMAGE_COLUMN));
if (bytes != null) {
ImageView iv = (ImageView) v.findViewById(R.id.profile_icon);
iv.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
Having a black image usually is a symptom of a problem with alpha blended/semi transparent images. I would download manually one of those images and examine it with some image editing application to figure out what is the exact format of that image.
I believe that my issue was due to an Android bug. The same image displays fine on Windows. I believe I found others that were having the same issue intermittently on Android.
精彩评论