安卓:问题/错误与ThreadSafeClientConnManager下载图片 [英] Android: Problem/bug with ThreadSafeClientConnManager downloading images

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

问题描述

有关我当前的应用程序,我收集不同的事件图像 在西班牙的供应商。

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.

我原来的code(上图)被利用的<一个href="http://developer.android.com/reference/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html"相对=nofollow> 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.

我希望有使用的解决方案,既然我preFER的 ThreadSafeClientConnManager ,而不是的URLConnection 的。

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

更新3:

如建议由下面麦克莫舍,似乎通过使用 BufferedHttpEntity 中的解码错误的不会出现了。然而现在,虽然不经常,甚至比以前,我收到了 SkImageDe codeR ::厂返回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.

推荐答案

斯特凡,

我有同样的问题,并没有发现太多搜索互联网。很多人都有过这样的问题,但不是很多答案来解决它。

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.de codeStream是有问题的图像进行解码。

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.

我改变了我的code,以反映原来的code(使用HTT prequest)。我做了一个变化,我发现在<一个href="http://groups.google.com/group/android-developers/browse%5Fthread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot" rel="nofollow">http://groups.google.com/group/android-developers/browse%5Fthread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot (感谢Nilesh)。您需要添加BufferedHttpEntity bufHttpEntity =新BufferedHttpEntity(实体);

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%5Fthread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot (thanks Nilesh). You need to add "BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); "

下面是我的previous code:

Here was my previous code:

    	conn = (HttpURLConnection) bitmapUrl.openConnection(); 
    	conn.connect();
    	is = conn.getInputStream();
    	//bis = new BufferedInputStream(is);
    	//bm = BitmapFactory.decodeStream(bis);
    	bm = BitmapFactory.decodeStream(is);

和她是code的作品:

And her is the code that works:

            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张图片的网页,你可以刷新看到最新的照片。我会几乎一半失败,去codeR->德code返回false错误。有了上面code,我有没有问题。

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.

感谢

这篇关于安卓:问题/错误与ThreadSafeClientConnManager下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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