http://doinone.iteye.com/blog/1074366



// 如果缓存里面有就从缓存获取,否则网络获取图片,返回Drawable对象public static Drawable loadImageFromNetwork(Context context, String imageUrl){Drawable drawable = null;if(imageUrl == null )return null;String imagePath = "";String   fileName   = "";// 获取url中图片的文件名与后缀if(imageUrl!=null&&imageUrl.length()!=0){             fileName  = imageUrl.substring(imageUrl.lastIndexOf("/")+1);}// 图片在手机本地的存放路径,注意:fileName为空的情况imagePath = context.getCacheDir() + "/" + fileName;Log.i("test","imagePath = " + imagePath);File file = new File(context.getCacheDir(),fileName);// 保存文件Log.i("test","file.toString()=" + file.toString());if(!file.exists()&&!file.isDirectory()){try {// 可以在这里通过文件名来判断,是否本地有此图片FileOutputStream   fos=new   FileOutputStream( file );InputStream is = new URL(imageUrl).openStream();int   data = is.read(); while(data!=-1){         fos.write(data);         data=is.read();; } fos.close();is.close();//drawable = Drawable.createFromStream(//new URL(imageUrl).openStream(), file.toString() ); // (InputStream) new URL(imageUrl).getContent();drawable = Drawable.createFromPath(file.toString());Log.i("test", "file.exists()不文件存在,网上下载:" + drawable.toString());} catch (IOException e) {Log.d("test", e.getMessage());}}else{drawable = Drawable.createFromPath(file.toString());Log.i("test", "file.exists()文件存在,本地获取");}if (drawable == null) {Log.d("test", "null drawable");} else {Log.d("test", "not null drawable");}return drawable ;}


更多相关文章

  1. 关于Android用户定位获取location值为null的解决方法
  2. Android(安卓)获取Inflate得到的view的宽度高度
  3. Android(安卓)APP常用图标尺寸规范
  4. Android: 缩放图片文件引起的OOM异常
  5. Android(安卓)清理缓存、优化内存空间
  6. AccountManager getAccount 在Android(安卓)O 8.0版本中获取为 n
  7. android bitmap drawable 互转
  8. Android常用UI
  9. android手机内部存储空间和外部存储空间获取代码

随机推荐

  1. Android(安卓)Things正式版发布啦
  2. android 通知播放系统声音
  3. 使用charles proxy for Mac来抓取手机App
  4. Android使用Intent返回上一个Activity时S
  5. 关于Android(安卓)Studio开发环境变量的
  6. android:shape妙用
  7. Android控件RecyclerView和ListView的异
  8. 关于解决android studio各种疑难杂症系列
  9. 12.1 學習筆記 html div塊邊框圓角,footer
  10. 【自定义View系列】android的UI结构