- 浏览: 18221 次
最新评论
-
ygswine:
很是感谢, 你的博文对我很有用. 谢谢
android下如何设置系统时间 -
jyh149129:
我android新手,能否给我个ViewFlipper 调用的 ...
异步下载图片+图片缓存 -
iwangxiaodong:
补充一个Android 应用视觉效果设计技巧:http://w ...
android小技巧收集 -
573842281:
压缩包里面没有什么东西啊!
仿淘宝"摇一摇"
代码参考自:jamendo,有一定修改。
功能如下:
流程如下:
RemoteImageViewActivity:
public class RemoteImageViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RemoteImageView img = (RemoteImageView) findViewById(R.id.remote_img); img.setDefaultImage(R.drawable.ic_launcher); img.setImageUrl("http://img2.kwcdn.kuwo.cn:81/star/albumcover/120/7/8/83787_1323997225.jpg"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); } }
ImageCache:
public class ImageCache extends WeakHashMap<String, Bitmap>{ /** * 判断该url是否存在 * @param url * @return */ public boolean isCached(String url){ return containsKey(url) && get(url) != null; } }
RemoteImageApplication:
public class RemoteImageApplication extends Application { public static final String TAG = "RemoteImageApplication"; private static RemoteImageApplication application; private ImageCache mImageCache; public SharedPreferences prefs = null; public static RemoteImageApplication getInstance() { return application; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); application = this; mImageCache = new ImageCache(); prefs = PreferenceManager.getDefaultSharedPreferences(this); } public ImageCache getImageCache() { return mImageCache; } }
RemoteSettings:
public class RemoteSettings { public static final String CACHE_SIZE = "cache_size"; //图片缓存保留大小,如果超过该大小,即进行自动清除缓存. }
RemoteImageView:
public class RemoteImageView extends ImageView { private Context mContext; private static int mCacheSize = 150; // 设置的缓存大小。 private static final int MAX_FAILURES = 3; // 下载的尝试请求次数 private int mFailure; // 下载失败次数 private String mUrl; // 当前下载的url private String mCurrentlyGrabbedUrl; // 当前下载成功的url private final static String JAMENDO_DIR = "Android/data/com.teleca.jamendo"; // 文件缓存存放的路径. private final static long MB = 1073741824; public RemoteImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; } public RemoteImageView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public RemoteImageView(Context context) { super(context); mContext = context; } /** * 设置默认图片 */ public void setDefaultImage(Integer resid) { setImageResource(resid); } /** * 设置需要异步加载的图片 */ public void setImageUrl(String url) { // 下载失败进行重试,如果重试次数超过规定的限制,则直接返回. if (mUrl != null && mUrl.equals(url) && (mCurrentlyGrabbedUrl == null || (mCurrentlyGrabbedUrl != null && !mCurrentlyGrabbedUrl .equals(url)))) { mFailure++; if (mFailure > MAX_FAILURES) { Log.e(RemoteImageApplication.TAG, "下载该图片地址失败:" + url); return; } } else { mUrl = url; mFailure = 0; } ImageCache imageCache = RemoteImageApplication.getInstance() .getImageCache(); if (imageCache.isCached(url)) { setImageBitmap(imageCache.get(url)); } else { // 如果内存中没有该缓存,则从文件中进行查找. String fileName = convertUrlToFileName(url); // 进行文件名处理 String filepath = getDirectory(fileName); // 取得缓存文件夹目录 String pathFileName = filepath + "/" + fileName; // 组拼文件 File pathFile = new File(pathFileName); if (!pathFile.exists()) { try { pathFile.createNewFile(); } catch (IOException e) { Log.d(RemoteImageApplication.TAG, "创建图片文件失败:" + pathFileName); } } Bitmap tbmp = BitmapFactory.decodeFile(pathFileName); if (tbmp == null) { Log.d(RemoteImageApplication.TAG, "图片文件不存在,开始进行下载"); try { new DownloadTask().execute(url); } catch (RejectedExecutionException e) { Log.d(RemoteImageApplication.TAG, "下载失败"); } } else { Log.i(RemoteImageApplication.TAG, "从文件中加载图片"); RemoteImageApplication.getInstance().getImageCache() .put(url, tbmp); this.setImageBitmap(tbmp); } updateCacheSize(pathFileName); // 进行检测文件大小,以便于清除缓存. } } private void updateCacheSize(String pathFileName) { // TODO Auto-generated method stub updateSizeCache(pathFileName); } /** * 检查文件目录是否超过规定的缓存大小 * * @param fileName */ private void updateSizeCache(String pathFileName) { // TODO Auto-generated method stub mCacheSize = PreferenceManager.getDefaultSharedPreferences(mContext) .getInt(RemoteSettings.CACHE_SIZE, 100); // 读取设置的缓存大小,前台可以动态设置此值 if (isSDCardEnable()) { String extStorageDirectory = Environment .getExternalStorageDirectory().toString(); // 取得SD根路径 String dirPath = extStorageDirectory + "/" + JAMENDO_DIR + "/imagecache"; File dirFile = new File(dirPath); File[] files = dirFile.listFiles(); long dirSize = 0; for (File file : files) { dirSize += file.length(); } if (dirSize > mCacheSize * MB) { clearCache(); } } } /** * 异步下载图片 * * @ClassName: DownloadTask * @author 姜涛 * @version 1.0 2012-1-15 下午5:06:21 */ class DownloadTask extends AsyncTask<String, Void, String> { private String mTaskUrl; private Bitmap mBmp = null; @Override public void onPreExecute() { // loadDefaultImage(); super.onPreExecute(); } @Override public String doInBackground(String... params) { mTaskUrl = params[0]; InputStream stream = null; URL imageUrl; Bitmap bmp = null; try { imageUrl = new URL(mTaskUrl); try { stream = imageUrl.openStream(); bmp = BitmapFactory.decodeStream(stream); try { if (bmp != null) { mBmp = bmp; RemoteImageApplication.getInstance() .getImageCache().put(mTaskUrl, bmp); Log.d(RemoteImageApplication.TAG, "图片缓存到application中: " + mTaskUrl); } } catch (NullPointerException e) { Log.w(RemoteImageApplication.TAG, "下载失败,图片为空:" + mTaskUrl); } } catch (IOException e) { Log.w(RemoteImageApplication.TAG, "无法加载该url:" + mTaskUrl); } finally { try { if (stream != null) { stream.close(); } } catch (IOException e) { } } } catch (MalformedURLException e) { e.printStackTrace(); } return mTaskUrl; } @Override public void onPostExecute(String url) { super.onPostExecute(url); Bitmap bmp = RemoteImageApplication.getInstance().getImageCache() .get(url); if (bmp == null) { Log.w(RemoteImageApplication.TAG, "尝试重新下载:" + url); RemoteImageView.this.setImageUrl(url); } else { RemoteImageView.this.setImageBitmap(bmp); mCurrentlyGrabbedUrl = url; saveBmpToSd(mBmp, url); } } }; /** * 把图片保存到本地 * * @param bm * @param url */ private void saveBmpToSd(Bitmap bm, String url) { if (bm == null) { return; } if (mCacheSize == 0) { return; } String filename = convertUrlToFileName(url); String dir = getDirectory(filename); File file = new File(dir + "/" + filename); try { file.createNewFile(); OutputStream outStream = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); Log.i(RemoteImageApplication.TAG, "图片已保存到sd卡"); } catch (FileNotFoundException e) { Log.w(RemoteImageApplication.TAG, "无法找到文件目录"); } catch (IOException e) { Log.w(RemoteImageApplication.TAG, "操作文件出错"); } } /** * 组拼文件名,后缀名用dat代替,避免别人使用图片管理器搜索出这种对于她们无用的图片. * * @param url * @return */ private String convertUrlToFileName(String url) { String filename = url; filename = filename.replace("http://", ""); filename = filename.replace("/", "."); filename = filename.replace(":", "."); filename = filename.replace("jpg", "dat"); filename = filename.replace("png", "dat"); return filename; } /** * 返回缓存图片所存放的文件夹 * * @param filename * @return */ private String getDirectory(String filename) { String extStorageDirectory = Environment.getExternalStorageDirectory() .toString(); // 取得SD根路径 String dirPath = extStorageDirectory + "/" + JAMENDO_DIR + "/imagecache"; File dirFile = new File(dirPath); if (!dirFile.exists()) { dirFile.mkdirs(); } return dirPath; } /** * 清除缓存 */ private void clearCache() { if (isSDCardEnable()) { String extStorageDirectory = Environment .getExternalStorageDirectory().toString(); // 取得SD根路径 String dirPath = extStorageDirectory + "/" + JAMENDO_DIR + "/imagecache"; File dir = new File(dirPath); File[] files = dir.listFiles(); // 取得该目录下的所有文件 if (files == null || files.length == 0) { return; } for (File file : files) { file.delete(); } Log.d(RemoteImageApplication.TAG, "已清除缓存:" + dirPath); } } /** * 判断SD卡是否可用 */ public static boolean isSDCardEnable() { return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } }
代码参见附件.
发表评论
-
android下如何设置系统时间
2013-04-09 23:58 5883最近做项 ... -
android小技巧收集
2012-08-15 05:15 2034一: 为应用程序设置 ... -
Android退出整个应用的方法
2012-08-13 03:10 1512对于网上所提供的杀进程,杀包等方式不予评价,这里提供一下 ... -
我的音乐播放器之旅(2)-模版化框架开发
2012-02-22 11:39 0从android3.0开始,引入了Action Bar的概念, ... -
我的音乐播放器之旅(1)-模仿思路
2012-02-21 22:23 77一:选型 从有做 ... -
我的音乐播放器之旅---索引贴
2012-02-21 17:37 66最近公司项目比较闲,想做点东西练练手,顺便把这么久时间来做an ... -
仿淘宝"摇一摇"
2012-02-02 13:41 1428前几天看到淘宝最新的客户端有一个弱网络情况数据加载超时,手持摇 ... -
对AsyncTask进行封装,简化繁琐的异步操作。
2012-01-17 11:33 3384对于android的异步操作,往往有二种方法给予我们 ... -
对AsyncTask进行封装,简化繁琐的异步操作。
2012-01-17 11:10 0对于android的异步操作,往往有二种方法给予我们 ...
相关推荐
ListView GridView ViewPager 异步加载网络图片 +缓存+可配置 万能图片加载器 超级顺畅加载显示 使用简单 教程地址:http://blog.csdn.net/zabio/article/details/19836805
ListView的异步加载图片并缓存,可以实现异步加载数据,异步加载图片,缓存图片,并且实现退出删除图片缓存
1,通过异步加载,避免阻塞UI线程 2,通过LruCache,将已经下载的图片放到内存中
android图片异步加载+本地缓存+内存缓存通用jar包(含源码和文档)
很多朋友在Android开发listview过程中都会碰到问题 比如从网上下载图片显示在界面 如果图片稍微大一点就会出现oom 或者显示的时候很容易出现图片闪烁等 这个项目能帮你解决
异步批量下载图片并缓存
实现图片下载和缓存功能,可以将图片下载之后缓存到内存或者SD卡中。图片第一次从互联网下载之后,就会缓存到内存或者SD卡中。第二次再打开浏览时,就无需再从互联网下载,而是直接从内存或者SD卡中读取。 效果图说...
两个例子: (1)GridView+异步请求图片 (2)ListView+异步请求图片 用HashMap, SoftReference<Drawable>> 来存储缓存图片,以便再次加载
使用 内存缓存与硬盘缓存 AsyncTask 异步下载图片并用GridView显示
使用的时候图片路径替换成自己的
这是一个异步加载 图片缓存管理的小例子 简化对网络图片加载的处理
两个工程,android图片的异步下载和缓存。
iOS 图片异步加载 图片缓存
安卓源码包android图片缓存&展示Android 异步加载图片等24个合集: ‘360全景查看demo.rar afinal框架实现图片的简单异步缓存加载.rar andengine中直接加载多张小图片合成一张大图片生成动画精灵.rar android gif...
本项目是一个批量下载图片的小例子,项目编码UTF-8编译版本4.4.2主要特点有: 1、下载大图decode时,可根据View大小自动缩放图片,不在出现OOM和SkImageDecoder::Factory returned null错误 2、图片下载失败时,可...
Android 异步加载图片缓存优化能异步加载图片,并缓存到本地,采用一级缓存,二级缓存和本地缓存,避免oom异常。源码中有详细注释,资料中有jar包,可以直接复制到项目中使用。
SDWebImage异步下载缓存图片(支持离线)
演化理解 Android 异步加载图片
该代码用oc实现了异步下载网上图片的瀑布流照片墙,使用图片缓存机制