标签:des android style blog http color io os ar
Image_url = new StringBuffer(AppConstant.DOWNLOAD_IMAGE_URL) .append(msg.getOPImageList().get(0).getImageUrl()) .toString(); ImageView imageView = holder.msgImage; imageView.setTag(Image_url); Bitmap bitmap = Loader.loadBitmap(imageView, Image_url, new ImageCallBack() { @Override public void imageLoad(ImageView imageView, Bitmap bitmap) { if (imageView.getTag() != null && imageView.getTag().equals(Image_url)) { imageView.setImageBitmap(bitmap); } } }); Log.e("当前的postion", "" + position); if (bitmap == null) { imageView.setImageDrawable(context.getResources().getDrawable( R.drawable.ic_launcher)); } else { imageView.setImageBitmap(bitmap); }
public class Loader { /** * 内存图片软引用缓冲 */ private static HashMap<String, SoftReference<Bitmap>> imageCache = null; public Loader() { imageCache = new HashMap<String, SoftReference<Bitmap>>(); } public static String getFileName(String url) { return url.substring(url.lastIndexOf(File.separator) + 1); } public static Bitmap getLocalResource(String destDir, String imageName) { Bitmap bmp = null; File imgeDir = new File(destDir); File cache = null; if (!imgeDir.exists()) { // 判断本地缓存目录是否存在 imgeDir.mkdirs(); } else { cache = new File(destDir + File.separator + imageName); // 判断该图片资源是否存在 if (cache.exists()) { // 如果存在就根据图片的存储路径得到Bitmap对象 bm bmp = BitmapFactory.decodeFile(cache.getAbsolutePath()); } } return bmp; } public static Bitmap loadBitmap(final ImageView imageView, final String imageURL, final ImageCallBack imageCallBack) { // 在内存缓存中,则返回Bitmap对象 if (imageCache.containsKey(imageURL)) { SoftReference<Bitmap> reference = imageCache.get(imageURL); Bitmap bitmap = reference.get(); if (bitmap != null) { return bitmap; } } else { /** * 加上一个对本地缓存的查找 */ String bitmapName = imageURL .substring(imageURL.lastIndexOf("/") + 1); Bitmap bitmapTemp = null; bitmapTemp = getLocalResource(AppConstant.TEST, bitmapName); if (bitmapTemp != null) { return bitmapTemp; } } final Handler handler = new Handler() { /* * (non-Javadoc) * * @see android.os.Handler#handleMessage(android.os.Message) */ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub imageCallBack.imageLoad(imageView, (Bitmap) msg.obj); } }; // 如果不在内存缓存中,也不在本地(被jvm回收掉),则开启线程下载图片 new Thread() { /* * (non-Javadoc) * * @see java.lang.Thread#run() */ @Override public void run() { // TODO Auto-generated method stub Bitmap bitmap = null; try { URL imageUrl = new URL(imageURL); HttpURLConnection conn = (HttpURLConnection) imageUrl .openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); // InputStream is = conn.getInputStream(); InputStream in = conn.getInputStream(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inSampleSize = 10; // width,hight设为原来的十分一 bitmap = BitmapFactory.decodeStream(in, null, options); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap)); Message msg = handler.obtainMessage(0, bitmap); handler.sendMessage(msg); File dir = new File(AppConstant.TEST); if (!dir.exists()) { dir.mkdirs(); } File bitmapFile = new File(AppConstant.TEST + File.separator + imageURL.substring(imageURL.lastIndexOf("/") + 1)); if (!bitmapFile.exists()) { try { bitmapFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } FileOutputStream fos; try { fos = new FileOutputStream(bitmapFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); return null; } /** * 回调接口 * * @author onerain * */ public interface ImageCallBack { public void imageLoad(ImageView imageView, Bitmap bitmap); } }
标签:des android style blog http color io os ar
原文地址:http://www.cnblogs.com/lianxu61/p/4002195.html