Created
October 20, 2013 20:07
-
-
Save cmendesce/7074623 to your computer and use it in GitHub Desktop.
Class for downloading and storing images in android project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.lang.ref.SoftReference; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.WeakHashMap; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import android.graphics.Bitmap; | |
| import android.graphics.BitmapFactory; | |
| import android.os.Handler; | |
| import android.os.Message; | |
| import android.widget.ImageView; | |
| public class BitmapManager { | |
| private final Map<String, SoftReference<Bitmap>> cache; | |
| private final ExecutorService pool; | |
| private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>()); | |
| private Bitmap placeholder; | |
| private static BitmapManager instance = new BitmapManager(); | |
| private String url; | |
| public static BitmapManager getInstance() { | |
| return instance; | |
| } | |
| private BitmapManager() { | |
| cache = new HashMap<String, SoftReference<Bitmap>>(); | |
| pool = Executors.newFixedThreadPool(5); | |
| } | |
| public void setPlaceholder(Bitmap bmp) { | |
| placeholder = bmp; | |
| } | |
| private Bitmap getBitmapFromCache(String url) { | |
| if (cache.containsKey(url)) { | |
| return cache.get(url).get(); | |
| } | |
| return null; | |
| } | |
| public void queueJob(final String url, final ImageView imageView) { | |
| /* Create handler in UI thread. */ | |
| final Handler handler = new Handler() { | |
| @Override | |
| public void handleMessage(Message msg) { | |
| String tag = imageViews.get(imageView); | |
| if (tag != null && tag.equals(url)) { | |
| if (msg.obj != null) { | |
| imageView.setImageBitmap((Bitmap) msg.obj); | |
| } else { | |
| imageView.setImageBitmap(placeholder); | |
| Log.d(null, "fail " + url); | |
| } | |
| } | |
| } | |
| }; | |
| pool.submit(new Runnable() { | |
| public void run() { | |
| final Bitmap bmp = downloadBitmap(url); | |
| Message message = Message.obtain(); | |
| message.obj = bmp; | |
| Logg.d(null, "Item downloaded: " + url); | |
| handler.sendMessage(message); | |
| } | |
| }); | |
| } | |
| public void loadBitmap(final String url, final ImageView imageView) { | |
| imageViews.put(imageView, url); | |
| Bitmap bitmap = getBitmapFromCache(url); | |
| // check in UI thread, so no concurrency issues | |
| if (bitmap != null) { | |
| Log.d(null, "Item loaded from cache: " + url); | |
| imageView.setImageBitmap(bitmap); | |
| } else { | |
| imageView.setImageBitmap(placeholder); | |
| queueJob(url, imageView); | |
| } | |
| } | |
| private Bitmap downloadBitmap(String url) { | |
| try { | |
| Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); | |
| cache.put(url, new SoftReference<Bitmap>(bitmap)); | |
| return bitmap; | |
| } catch (MalformedURLException e) { | |
| Log.d(null, e.getMessage()); | |
| } catch (IOException e) { | |
| Log.d(null, e.getMessage()); | |
| } | |
| return null; | |
| } | |
| /** | |
| * Sets image url | |
| * @param image url | |
| */ | |
| public final BitmapManager from(final String url) { | |
| this.url = url; | |
| return getInstance(); | |
| } | |
| /** | |
| * Performs image download | |
| * @param imageView | |
| */ | |
| public final void into(ImageView imageView) { | |
| loadBitmap(url, imageView); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
BitmapManager.getInstance().from("www.server.com.br/image.png").into(myImageView);