ImageDownloader를 사용할때, statuscode가 302라 이미지 출력이 안될때…

그냥 이것도 저를 위한 메모입니다만, 혹시 저같이 초보인 다른 분들이 헤매실까봐 공개로 해 놓습니다.


NO_ASYNC_TASK일때는 상관 없는데(NO_ASYNC_TASK쓸일은 캐시때문이겠지만…), 


NO_DOWNLOADED_DRAWABLE이나 CORRET 사용시입니다.


페이스북api같이 프로필 사진등을 리다이렉션 해주는 주소로 줄때 이러한일이 발생하는데요.




ImageDownloader클래스의 Bitmap downloadBitmap(String url) 메서드를 수정해 주시면 됩니다.

    Bitmap downloadBitmap(String url) {
final int IO_BUFFER_SIZE = 4 * 1024;

// AndroidHttpClient is not allowed to be used from the main thread
final HttpClient client = (mode == Mode.NO_ASYNC_TASK) ? new DefaultHttpClient() :
AndroidHttpClient.newInstance(“Android”);
final HttpGet getRequest = new HttpGet(url);

try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 301 || statusCode == 302)
{
Header redirect = response.getFirstHeader(“Location”);
if (client instanceof AndroidHttpClient)
((AndroidHttpClient)client).close();
return downloadBitmap(redirect.getValue());
}
if (statusCode != HttpStatus.SC_OK) {
Log.w(“ImageDownloader”, “Error ” + statusCode +
” while retrieving bitmap from ” + url);
return null;
}

final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
// return BitmapFactory.decodeStream(inputStream);
// Bug on slow connections, fixed in future release.
return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (IOException e) {
getRequest.abort();
Log.w(LOG_TAG, “I/O error while retrieving bitmap from ” + url, e);
} catch (IllegalStateException e) {
getRequest.abort();
Log.w(LOG_TAG, “Incorrect URL: ” + url);
} catch (Exception e) {
getRequest.abort();
Log.w(LOG_TAG, “Error while retrieving bitmap from ” + url, e);
} finally {
if ((client instanceof AndroidHttpClient)) {
((AndroidHttpClient) client).close();
}
}
return null;
}


강조된 부분이 추가된 부분입니다. (Line 12 ~ 18)


 저도 잘 모르는지라, 질문하셔도 답은 못할 수 있습니다. 




참고 :




6 thoughts on “ImageDownloader를 사용할때, statuscode가 302라 이미지 출력이 안될때…”

cyko2에 답글 남기기 응답 취소

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.