标签:des android blog http java 使用 os io
异步下载网络图片,并提供是否缓存至内存或外部文件的功能
异步加载类AsyncImageLoader
public void downloadImage(final String url, final ImageCallback callback);
public void downloadImage(final String url, final boolean cache2Memory, final ImageCallback callback);
public void setCache2File(boolean flag);
public void setCachedDir(String dir);
图片下载和缓存实现类LoaderImpl
1.AsyncImageLoader.java
- package com.imagecache;
-
- import java.lang.ref.SoftReference;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.os.Handler;
- import android.util.Log;
-
- public class AsyncImageLoader {
-
- private static HashSet<String> sDownloadingSet;
-
- private static Map<String,SoftReference<Bitmap>> sImageCache;
-
- private static LoaderImpl impl;
-
- private static ExecutorService sExecutorService;
-
-
- private Handler handler;
-
-
-
- public interface ImageCallback{
-
- public void onImageLoaded(Bitmap bitmap, String imageUrl);
- }
-
- static{
- sDownloadingSet = new HashSet<String>();
- sImageCache = new HashMap<String,SoftReference<Bitmap>>();
- impl = new LoaderImpl(sImageCache);
- }
-
- public AsyncImageLoader(Context context){
- handler = new Handler();
- startThreadPoolIfNecessary();
-
- String defaultDir = context.getCacheDir().getAbsolutePath();
- setCachedDir(defaultDir);
- }
-
-
- public void setCache2File(boolean flag){
- impl.setCache2File(flag);
- }
-
-
- public void setCachedDir(String dir){
- impl.setCachedDir(dir);
- }
-
-
- public static void startThreadPoolIfNecessary(){
- if(sExecutorService == null || sExecutorService.isShutdown() || sExecutorService.isTerminated()){
- sExecutorService = Executors.newFixedThreadPool(3);
-
- }
- }
-
-
- public void downloadImage(final String url, final ImageCallback callback){
- downloadImage(url, true, callback);
- }
-
-
- public void downloadImage(final String url, final boolean cache2Memory, final ImageCallback callback){
- if(sDownloadingSet.contains(url)){
- Log.i("AsyncImageLoader", "###该图片正在下载,不能重复下载!");
- return;
- }
-
- Bitmap bitmap = impl.getBitmapFromMemory(url);
- if(bitmap != null){
- if(callback != null){
- callback.onImageLoaded(bitmap, url);
- }
- }else{
-
- sDownloadingSet.add(url);
- sExecutorService.submit(new Runnable(){
- @Override
- public void run() {
- final Bitmap bitmap = impl.getBitmapFromUrl(url, cache2Memory);
- handler.post(new Runnable(){
- @Override
- public void run(){
- if(callback != null)
- callback.onImageLoaded(bitmap, url);
- sDownloadingSet.remove(url);
- }
- });
- }
- });
- }
- }
-
-
- public void preLoadNextImage(final String url){
-
- downloadImage(url, null);
- }
-
- }
2.LoaderImpl.java
3.测试Activity
- package com.imagecache;
-
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.widget.ImageView;
-
- public class MainActivity extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- final ImageView iv = (ImageView)findViewById(R.id.iv);
-
- String imgUrl = "http://...";
-
-
- AsyncImageLoader loader = new AsyncImageLoader(getApplicationContext());
-
-
- loader.setCache2File(true);
-
- loader.setCachedDir(this.getCacheDir().getAbsolutePath());
-
-
- loader.downloadImage(imgUrl, true
- @Override
- public void onImageLoaded(Bitmap bitmap, String imageUrl) {
- if(bitmap != null){
- iv.setImageBitmap(bitmap);
- }else{
-
- }
- }
- });
- }
-
- }
转自:http://blog.csdn.net/liliang497/article/details/7221110
android下载网络图片并缓存,布布扣,bubuko.com
android下载网络图片并缓存
标签:des android blog http java 使用 os io
原文地址:http://www.cnblogs.com/pricks/p/3921611.html