안드로이드 개발 질문/답변
(글 수 45,052)
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
public String url;
public String targetUrl;
private WeakReference<ImageView> imageViewReference;
public ImageDownloaderTask(String url, ImageView imageView)
{
this.targetUrl = url;
this.imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params)
{
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
if(isCancelled())
{
bitmap = null;
}
if(imageViewReference != null)
{
ImageView imageView = imageViewReference.get();
ImageDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if(this == bitmapDownloaderTask)
{
ImageDownloader.mImageCache.put(targetUrl, bitmap);
imageView.setImageBitmap(bitmap);
}
}
}
private ImageDownloaderTask getBitmapDownloaderTask(ImageView imageView)
{
if(imageView != null)
{
Drawable drawable = imageView.getDrawable();
if(drawable instanceof DownloadedDrawable)
{
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
static Bitmap downloadBitmap(String url)
{
final HttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try
{
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
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;
//BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 2;
try
{
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
//final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream), null, options);
return bitmap;
}
finally
{
if(inputStream != null)
{
inputStream.close();
}
entity.consumeContent();
}
}
}
catch(Exception e)
{
getRequest.abort();
}
return null;
}
static class FlushedInputStream extends FilterInputStream
{
public FlushedInputStream(InputStream inputStream)
{
super(inputStream);
}
@Override
public long skip(long n) throws IOException
{
long totalBytesSkipped = 0L;
while(totalBytesSkipped < n)
{
long bytesSkipped = in.skip(n - totalBytesSkipped);
if(bytesSkipped == 0L)
{
int bytes = read();
if(bytes < 0)
{
break; // we reached EOF
}
else
{
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
}
DB에서 stringd으로 저장된 url을 string으로 받아서 이미지를 받아옵니다.
그런데 task쪽에서 에러가 나옵니다.
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
로그가 찍히는데 statusCode에 404가 찍히네요.
이거 무슨 뜻인가요?
2012.08.03 09:24:02
ImageDownloaderTask start()를 실행하기 전, background 등 여러부분에
URL 로그를 찍어 보시길바랍니다.
전혀 다른부분에서 원인이 나올수도 있으니깐요.
그리고 downloadBitmap 함수를 static 제거해보시고
final HttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
에서 final을 제거해 보시길 바랍니다.
객체생성으로 예전에 제대로 안됐던 경험이 있어서... 조언을 해봅니다.
정확한 해결책은 아니니 참고만 해보세요 ^^
에서 final을 제거해 보시길 바랍니다.
객체생성으로 예전에 제대로 안됐던 경험이 있어서... 조언을 해봅니다.
정확한 해결책은 아니니 참고만 해보세요 ^^




statusCode 404 는 페이지를 찾을 수 없다는 에러입니다.
해당 이미지 URL 이 잘못되었거나 이미지가 존재하지 않는 경우입니다. (혹은 방화벽이나 서버에서 막아놓은 경우도 있습니다.)