为什么从 url 加载数据时某些图像显示为黑色?
我正在从 url 下载图像并将它们存储在 SQLite 数据库表中。这些图像是用户的个人资料图片。我将个人资料图像和用户信息存储在一行中。每个用户一行。
这大多有效。几乎所有图像都可以很好地加载到我的列表中。但是,大约 300 个用户中有 2-4 个显示黑色图像。我通过网络浏览器下载图像,验证了拥有黑色图像的用户在 url 上拥有有效的图像。
有人知道为什么这些少数用户显示黑色图像吗?我没有看到任何例外。
这是我的代码
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));
}
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 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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
黑色图像通常是 Alpha 混合/半透明图像出现问题的症状。我会手动下载其中一张图像,并使用一些图像编辑应用程序对其进行检查,以找出该图像的确切格式。
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.
我相信我的问题是由于 Android 错误造成的。相同的图像在 Windows 上显示良好。我相信我发现其他人在 Android 上间歇性地遇到同样的问题。
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.