標籤:des android style blog http color io os ar
============問題描述============
學做android,自己想模仿QQ空間做一個小demo
listview非同步載入圖片的時候遇到了一個問題
非同步載入用到了SoftReference 和檔案快取的方法
每次載入圖片的時候,也在記憶體或緩衝中找到了圖片
第一次載入出來後,listview滑動了,同樣也進到了setImageBitmap這一步
可是就是圖片顯示不出來變成空白
下面帖幾張圖和代碼
滑動前
滑動後
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() {@Overridepublic 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);}
loader
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對象 bmbmp = 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) */@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubimageCallBack.imageLoad(imageView, (Bitmap) msg.obj);}};// 如果不在記憶體緩衝中,也不在本地(被jvm回收掉),則開啟線程下載圖片new Thread() {/* * (non-Javadoc) * * @see java.lang.Thread#run() */@Overridepublic void run() {// TODO Auto-generated method stubBitmap 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 blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.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 blocke.printStackTrace();}}FileOutputStream fos;try {fos = new FileOutputStream(bitmapFile);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();return null;}/** * 回調介面 * * @author onerain * */public interface ImageCallBack {public void imageLoad(ImageView imageView, Bitmap bitmap);}}
坐等大神支招啊
每次都進了setimagebitmap這一步 就是載入不出圖片
============解決方案1============
你用imageLoader這個三方開源庫試試。
============解決方案2============
看一下你imageview控制項設的是src還是background。或者試試把bitmap轉成drawable,再setbackground試試
============解決方案3============
看下getView方法?
============解決方案4============
貼下Adapter的代碼看看唄
============解決方案5============
你試試AsyncImage這個第三方開源庫
============解決方案6============
ImageView imageViewByTag = (ImageView) listview
.findViewWithTag(imageUrl);
問題應該在這一句 你這個搜尋 只會找到第一個TAG 是imageUrl的控制項。 可能並不是你的要更新的控制項。
比如 你setTAG了 , listView 會重用item的, 在 msg.getOPImageList().size()=0 時候 還是有這個tag。
然後你find的結果就是這個contentView裡面的,不是你要更新的那個ImageView。
============解決方案7============
引用
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);
}
這個代碼中 在 ImageCallBack 介面中 吧imageUrl 回傳回來,跟imageView.getTag() 進行比較,應該可以解決問題。試試看。
android listview 非同步載入問題