android使用异步http连接来获取图像位图
我正在尝试使用异步请求从网址获取图像,以防止网址挂起。这是我为此使用的代码段
private void setImg(final ImageView im, String url){
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
public void onSuccess(String response){
try{
byte[] imageAsBytes = response.getBytes();
im.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
im.refreshDrawableState();
} catch(Throwable e){
e.printStackTrace();
}
}
});
}
这总是在 logcat 中显示此警告
12-29 01:55:33.043: D/skia(14414): --- SkImageDecoder::Factory returned null
我找不到合适的理由。有什么帮助吗?
I am trying to use async requests to fetch the images from urls, to prevent the url from hanging. This is the piece of code i am using for this
private void setImg(final ImageView im, String url){
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
public void onSuccess(String response){
try{
byte[] imageAsBytes = response.getBytes();
im.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
im.refreshDrawableState();
} catch(Throwable e){
e.printStackTrace();
}
}
});
}
This is always showing this warning in logcat
12-29 01:55:33.043: D/skia(14414): --- SkImageDecoder::Factory returned null
I cannot find a proper reason for this. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
现在,二进制响应处理程序已添加到 AsyncHttp,您可以简单地使用 androids BitmapFactory.decodeByeArray 函数:
Now that a binary response handler has been added to AsyncHttp you can simply use androids BitmapFactory.decodeByeArray function:
我最近一直在使用以下库:UrlImageViewHelper。它使用AsyncTask来下载图像。您的代码将是这样的:
现在很简单,对吗?
I've been using lately the following library: UrlImageViewHelper. It uses an AsyncTask to download the image. Your code would be something like this:
Very simple now, am I right?
如果有人仍在研究这个问题,这就是我的做法
If anyone is still working on this, here's how I am doing it
[更新]
图像(和任何二进制数据)响应处理已添加到loopj 的 android-async-http 库中。使用 BinaryHttpResponseHandler
[旧帖子]
Loopj 的 AsyncHttpClient 尚不支持处理 byte[] 响应。有一个叉子可以,但是很乱。所以答案是以下之一:
A1) 你不知道。
A2)您可以通过以下链接使用通过链接提供的分叉:https://github。 com/loopj/android-async-http/issues/8
A3) 您分叉 AsyncHttpClient,添加 byte[] 处理(不删除字符串响应处理,天哪!),然后将其提交回项目。通过这样做,您还将获得开源业力积分。
[UPDATE]
Image (and any binary data) response handling has been added to loopj's android-async-http library. Use the BinaryHttpResponseHandler
[old post]
loopj's AsyncHttpClient does not yet support handling of byte[] responses. There's a fork that does, but it's a mess. So the answer is one of these:
A1) You don't.
A2) You use the fork provided via link via this link: https://github.com/loopj/android-async-http/issues/8
A3) You fork AsyncHttpClient, add byte[] handling (without tearing out String response handling, jesus!), and commit it back to the project. By doing this you will also receive open-source karma credits.
Android 使用异步 http 连接获取图像和文本并显示在 listView 中
Android using async http connection to get image and text and show in a listView