Android:用ThreadSafeClientConnManager下载图片的Bug [英] Android: Bug with ThreadSafeClientConnManager downloading images

查看:131
本文介绍了Android:用ThreadSafeClientConnManager下载图片的Bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我目前的申请,我在西班牙收集来自不同event
供应商的图片。

For my current application I collect images from different "event providers" 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 following logcat 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.

我的原始代码(以上)正在利用 ThreadSafeClientConnManager 。如果我用一个简单的 URLConnection 替换它就可以完美地运行:

My original code (above) is taking advantage of the ThreadSafeClientConnManager. If I replace it with just a simple URLConnection it works perfectly:

URL url = new URL(strURL);
URLConnection conn = url.openConnection();
conn.connect();
InputStream instream = conn.getInputStream();
bmp = BitmapFactory.decodeStream(instream);

所以,我现在想知道为什么我的 ThreadSafeClientConnManager 与我的所有其他连接(主要是交换 JSONObjects )完美地工作(至少看起来确实如此)但不是来自某些特定网站的图像(例如salir.com - 对于大多数其他网站,它工作,但)。是否存在我缺少的HTTP参数?

So, I'm wondering now why does my ThreadSafeClientConnManager work flawlessly (at least it seems it does) with all my other connections (mostly exchanging JSONObjects) but not with images from some specific websites (e.g. salir.com - for most other websites it works, though). Is there a HTTP parameter I'm missing?

我当前的设置是:

HttpParams parameters = new BasicHttpParams();
HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(parameters, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(parameters, false); // some webservers have problems if this is set to true
ConnManagerParams.setMaxTotalConnections(parameters, MAX_TOTAL_CONNECTIONS);
HttpConnectionParams.setConnectionTimeout(parameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(parameters, SOCKET_TIMEOUT);

SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", 
     PlainSocketFactory.getSocketFactory(), HTTP_PORT));

ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg);

DefaultHttpClient http_client = new DefaultHttpClient(conMgr, parameters);






更新2:

现在,奇怪的是,它确实适用于 ThreadSafeClientConnManager -sometimes- 即可。如果我继续尝试下载图像并连续解码几次,它可能会在15-30次试验后起作用。非常奇怪。

Now, the strange thing is, that it actually does work with the ThreadSafeClientConnManager -sometimes-. If I keep trying downloading the image and decoding it for a couple of times in a row it might work after 15-30 trials. Very strange.

我希望有一个解决方案,因为我更喜欢使用 ThreadSafeClientConnManager 而不是 URLConnection

I hope there's a solution to that since I would prefer using the ThreadSafeClientConnManager instead of URLConnection.

更新3:

正如下面Mike Mosher所建议的那样,似乎是通过使用 BufferedHttpEntity 解码错误不再出现。但是现在,即使比以前少了,我得到一个 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)。我做了一个更改,我在 http: //groups.google.com/group/android-developers/browse_thread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot (感谢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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆