问题描述
对于我目前的申请,我在西班牙收集来自不同event
供应商的图片。
For my current application I collect images from different "eventproviders" in Spain.
Bitmap bmp=null;
HttpGet httpRequest = new HttpGet(strURL);
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
Log.i(TAG, "Image ["+ strURL + "] fetched in [" + (System.currentTimeMillis()-t) + "ms]");
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
bmp = BitmapFactory.decodeStream(instream);
return bmp;
然而,当从salir.com下载图像时,我得到以下
logcat输出:
However, when downloading images from salir.com I get the followinglogcat output:
13970 Gallery_Activity I Fetching image 2/8 URL: http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg
13970 ServiceHttpRequest I Image [http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg] fetched in [146ms]
13970 skia D --- decoder->decode returned false
搜索该错误消息没有提供太多有用的结果。
A search for that error message didn't provide much useful results.
任何人都知道问题可能是什么?
Anyone an idea what the problem could be?
格拉西亚斯!
更新1:
在询问了一些并测试不同的东西后,我发现问题似乎在于其他地方。即使我的logcat输出显示
After inquiring a bit more and testing different stuff I figured out that the problem seems to lie somewhere else. Even though my logcat output says
13970 ServiceHttpRequest I Image [http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg] fetched in [146ms]
getContentLength(): 93288
这是图像的正确长度,以字节为单位,似乎流或HTTP连接有问题。
which is the correct length of the image in bytes it seems that there's something wrong with the stream or HTTP connection.
我的原始代码(以上)正在利用 解码错误不再出现。但是现在,即使比以前少了,我得到一个 SkImageDecoder :: Factory返回null
错误。
As suggest by Mike Mosher below, it seems that by using BufferedHttpEntity the decoding error doesn't appear any more. However now, even though less often than before, I get a SkImageDecoder::Factory returned null
error.
推荐答案
Stefan,
我遇到了同样的问题,并没有找到太多的互联网搜索。很多人都有这个问题,但没有很多解决方案。
I've had the same issue, and didn't find much searching the internet. Many people have had this issue, but not alot of answers to solve it.
我使用URLConnection获取图像,但我发现问题不在于下载,而是BitmapFactory.decodeStream在解码图像时出现问题。
I was fetching images using URLConnection, but I found out the issue doesn't lie in the download, but the BitmapFactory.decodeStream was having an issue decoding the image.
我更改了我的代码以反映原始代码(使用httpRequest)。我做了一个更改,我在(感谢Nilesh)。你需要添加BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
I changed my code to reflect your original code (using httpRequest). I made one change, which I found at http://groups.google.com/group/android-developers/browse_thread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot (thanks Nilesh). You need to add "BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); "
这是我以前的代码:
conn = (HttpURLConnection) bitmapUrl.openConnection();
conn.connect();
is = conn.getInputStream();
//bis = new BufferedInputStream(is);
//bm = BitmapFactory.decodeStream(bis);
bm = BitmapFactory.decodeStream(is);
她的代码是有效的:
HttpGet httpRequest = null;
try {
httpRequest = new HttpGet(bitmapUrl.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bm = BitmapFactory.decodeStream(instream);
正如我所说,我有一个页面下载大约40张图片,你可以刷新看到最近的照片。我几乎有一半失败了decoder-> decode返回错误错误。使用上面的代码,我没有遇到任何问题。
As I said, I have a page that download around 40 images, and you can refresh to see the most recent photos. I would have almost half fail with the "decoder->decode returned false error". With the above code, I have had no problems.
谢谢
这篇关于Android:用ThreadSafeClientConnManager下载图片的Bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!