1,图片缓存用一个LinkedHashmap<String,SoftReference<Bitmap>> , 再存到本地. 需要时先判断map中有没有, 没有找本地文件, 再没有重新下载.
2,当getView中异步加载图片的时候, convert不需要重用, 转而用set来保存生成的view
3,getview方法内不要做获取数据的处理, getView就是用来get view的
4,客户端不管如何优化,如果返回的都是大图片的话,仍然会OOM. 有些时候需要客户端增加字段,判断该字段还返回不同大小的图片.
private String getBitmapName(String imageUrl){ String[] str = imageUrl.split("/"); return str[str.length-2]+str[str.length-1]+"xxx"; } public void loadBitmap(final String imageUrl, final ImageView imageview) { final Handler handler = new Handler() { public void handleMessage(Message msg) { imageview.setImageBitmap((Bitmap) msg.obj); }; }; final String bitmapName = getBitmapName(imageUrl); File localJPG = new File(localFolderName+bitmapName); // Log.v("0712 ", "获得到的图片名"+bitmapName); if (drawableMap.containsKey(imageUrl+"22")) { Log.e("0712 ", "直接用内存池的图片"); SoftReference<Bitmap> softReference = drawableMap.get(imageUrl); Bitmap bitmap = softReference.get(); if (bitmap != null) { // Message msg = handler.obtainMessage(0, bitmap); // handler.sendMessage(msg); imageview.setImageBitmap(bitmap); } } // else if(localJPG.exists()){ else if(false){ Log.e("0712 ", "去找SD卡中的图片"); BitmapDrawable drawable = (BitmapDrawable) BitmapDrawable.createFromPath(localFolderName+bitmapName); imageview.setImageDrawable(drawable); } else { Log.e("0712", "本地没有找到图片"); imageview.setImageDrawable(null); File cacheDir = new File(localFolderName); // File[] cacheFiles = cacheDir.listFiles(); // int i = 0; // if (cacheFiles != null) { // int count = cacheFiles.length; // for (; i < count; i++) // todo // { // Log.d("0712", "在循环找图片"); // if (bitmapName.equals(cacheFiles[i].getName())) { // break; // } // } // // if (i < count) { // // Message msg = handler.obtainMessage(0, // // BitmapFactory.decodeFile(localFolderName + // // bitmapName)); // // handler.sendMessage(msg); // // // imageview.setImageBitmap( // // // BitmapFactory.decodeFile(localFolderName + // // // imageUrl)); // // } // } Thread thread = new Thread() { public void run() { Log.e("0712", "new thread is running"); try { URL fileURL = new URL(imageUrl); HttpURLConnection conn; conn = (HttpURLConnection) fileURL.openConnection(); InputStream bitmapIs = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(bitmapIs); drawableMap.put(imageUrl, new SoftReference<Bitmap>(bitmap)); Message msg = handler.obtainMessage(0, bitmap); handler.sendMessage(msg); File dir = new File(localFolderName); if (!dir.exists()) { dir.mkdirs(); } File bitmapFile = new File(localFolderName+ bitmapName); 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, 80, fos); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }; }; MyThreadPoolManager.getInstance().startThread(imageUrl, thread); } }