안드로이드 원격 이미지 다운로더
Android 2015. 3. 20. 16:17http://theeye.pe.kr/archives/1309
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | public class ImageDownloader { public static final int IMGAE_CACHE_LIMIT_SIZE = 50; public static HashMap<String, Bitmap> mImageCache = new HashMap<String, Bitmap>(); public static void download(String url, ImageView imageView) { Bitmap cachedImage = mImageCache.get(url); if(cachedImage != null) { imageView.setImageBitmap(cachedImage); } else if(cancelPotentialDownload(url, imageView)) { if(mImageCache.size() > IMGAE_CACHE_LIMIT_SIZE) { mImageCache.clear(); } ImageDownloaderTask task = new ImageDownloaderTask(url, imageView); DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task); imageView.setImageDrawable(downloadedDrawable); task.execute(url); } } private static boolean cancelPotentialDownload(String url, ImageView imageView) { ImageDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView); if(bitmapDownloaderTask != null) { String bitmapUrl = bitmapDownloaderTask.url; if((bitmapUrl == null) || (!bitmapUrl.equals(url))) { bitmapDownloaderTask.cancel(true); } else { return false; } } return true; } private static 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 class DownloadedDrawable extends ColorDrawable { private final WeakReference<ImageDownloaderTask> bitmapDownloaderTaskReference; public DownloadedDrawable(ImageDownloaderTask bitmapDownloaderTask) { super(Color.TRANSPARENT); bitmapDownloaderTaskReference = new WeakReference<ImageDownloaderTask>(bitmapDownloaderTask); } public ImageDownloaderTask getBitmapDownloaderTask() { return bitmapDownloaderTaskReference.get(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 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; } } } | cs |