Android:用ThreadSafeClientConnManager下载图片的Bug

znq*_*znq 13 java android httpclient thread-safety bugfender

对于我目前的应用程序,我从西班牙的不同"活动提供商"收集图像.

  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;
Run Code Online (Sandbox Code Playgroud)

但是,从salir.com下载图像时,我得到以下logcat输出:

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
Run Code Online (Sandbox Code Playgroud)

搜索该错误消息没有提供太多有用的结果.

谁知道问题可能是什么?

格拉西亚斯!


更新1:

在查询了更多并测试不同的东西后,我发现问题似乎在于其他地方.即使我的logcat输出说

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
Run Code Online (Sandbox Code Playgroud)

这是图像的正确长度,以字节为单位,似乎流或HTTP连接有问题.

我的原始代码(上面)正在利用ThreadSafeClientConnManager.如果我用一个简单的替换它,URLConnection它完美地工作:

URL url = new URL(strURL);
URLConnection conn = url.openConnection();
conn.connect();
InputStream instream = conn.getInputStream();
bmp = BitmapFactory.decodeStream(instream);
Run Code Online (Sandbox Code Playgroud)

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

我目前的设置是:

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);
Run Code Online (Sandbox Code Playgroud)

更新2:

现在,奇怪的是,它实际上与ThreadSafeClientConnManager -sometimes-一起工作.如果我继续尝试下载图像并连续解码几次,它可能会在15-30次试验后起作用.很奇怪.

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


更新3:

正如Mike Mosher在下面的建议,似乎通过使用BufferedHttpEntity,解码错误不再出现.但是现在,即使比以前更少,我也会收到SkImageDecoder::Factory returned null错误.

小智 27

斯特凡,

我有同样的问题,并没有找到太多搜索互联网.很多人都有这个问题,但解决它的答案却很多.

我使用URLConnection获取图像,但我发现问题不在于下载,但BitmapFactory.decodeStream在解码图像时遇到问题.

我更改了我的代码以反映您的原始代码(使用httpRequest).我做了一个更改,我在http://groups.google.com/group/android-developers/browse_thread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot(感谢Nilesh)找到了这个更改.你需要添加"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);
Run Code Online (Sandbox Code Playgroud)

而她的代码是有效的:

            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);
Run Code Online (Sandbox Code Playgroud)

正如我所说,我有一个下载大约40张图像的页面,你可以刷新以查看最新的照片.我几乎有一半失败了"decoder-> decode返回错误错误".有了上面的代码,我没有遇到任何问题.

谢谢


cap*_*ler 5

我的解决方案是不使用BitmapFactory.decodeStream()因为我看它的方式,它使用SKIA解码器,它有时似乎有点不稳定.你可以尝试这样的事情.

    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
            in = new BufferedInputStream(new URL(url).openStream(),
                     IO_BUFFER_SIZE);

            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            copy(in, out);
            out.flush();

            final byte[] data = dataStream.toByteArray();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);


   } catch (......){

   }        
Run Code Online (Sandbox Code Playgroud)

以及copy()函数

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且IO_BUFFER_SIZE是一个常数,值为4*1024.


jfa*_*ell 0

我遇到了类似的问题,根本问题是由于请求某些图像时超时。我转而使用背景图像加载器,从那以后就没有出现任何问题。希望这可以帮助