Android: Bug with ThreadSafeClientConnManager downloading images

前端 未结 11 1471
我寻月下人不归
我寻月下人不归 2020-12-23 23:12

For my current application I collect images from different \"event providers\" in Spain.

  Bitmap bmp=null;
  HttpGet httpRequest = new HttpGet(strURL);

  l         


        
11条回答
  •  旧时难觅i
    2020-12-24 00:01

    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.

    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.

    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); "

    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);
    

    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);
    

    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.

    Thanks

提交回复
热议问题