2013-04-21 61 views
2

我想知道这是怎么回事。我的应用程序使用url加载图像。 我已经成功完成加载和显示。问题是, 时显示图像,显示错误的图像...... 这是更好地显示它的图像:Android - 使用URL加载图片,但加载后,其他图像替换其他图片

enter image description here

起初,同时仍在加载其他图像,正确的图像显示[TOP IMAGE]。 但加载完所有图像后,img2重复并替换img1,但仍使用img1的尺寸 ?

这是加载图像时我使用的代码:

public class DrawableBackgroundDownloader {  

    private final Map<String, Drawable> mCache = new HashMap<String, Drawable>(); 
    private final LinkedList <Drawable> mChacheController = new LinkedList <Drawable>(); 
    private ExecutorService mThreadPool; 
    private final Map<ImageView, String> mImageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>()); 
    public static int MAX_CACHE_SIZE = 80; 
    public int THREAD_POOL_SIZE = 3; 



    /** 
    * Constructor 
    */ 
    public DrawableBackgroundDownloader() { 
     mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE); 
    } 


    /** 
    * Clears all instance data and stops running threads 
    */ 
    public void Reset() { 
     ExecutorService oldThreadPool = mThreadPool; 
     mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE); 
     oldThreadPool.shutdownNow(); 

     mChacheController.clear(); 
     mCache.clear(); 
     mImageViews.clear(); 
    } 

    /** 
    * Load the drawable associated to a url and assign it to an image, you can set a placeholder to replace this drawable. 
    * @param url Is the url of the image. 
    * @param imageView The image to assign the drawable. 
    * @param placeholder A drawable that is show during the image is downloading. 
    */ 
    public void loadDrawable(final String url, final ImageView imageView,Drawable placeholder) { 
     if(!mImageViews.containsKey(url)) 
      mImageViews.put(imageView, url); 
     Drawable drawable = getDrawableFromCache(url); 

     // check in UI thread, so no concurrency issues 
     if (drawable != null) { 
      //Log.d(null, "Item loaded from mCache: " + url); 
      imageView.setImageDrawable(drawable); 
     } else { 
      imageView.setImageDrawable(placeholder); 
      queueJob(url, imageView, placeholder); 
     } 
    } 


    /** 
    * Return a drawable from the cache. 
    * @param url url of the image. 
    * @return a Drawable in case that the image exist in the cache, else returns null. 
    */ 
    public Drawable getDrawableFromCache(String url) { 
     if (mCache.containsKey(url)) { 
      return mCache.get(url); 
     } 

     return null; 
    } 


    /** 
    * Save the image to cache memory. 
    * @param url The image url 
    * @param drawable The drawable to save. 
    */ 
    private synchronized void putDrawableInCache(String url,Drawable drawable) { 
     int chacheControllerSize = mChacheController.size(); 
     if (chacheControllerSize > MAX_CACHE_SIZE) 
      mChacheController.subList(0, MAX_CACHE_SIZE/2).clear(); 

     mChacheController.addLast(drawable); 
     mCache.put(url, drawable); 

    } 

    /** 
    * Queue the job to download the image. 
    * @param url Image url. 
    * @param imageView The ImageView where is assigned the drawable. 
    * @param placeholder The drawable that is show during the image is downloading. 
    */ 
    private void queueJob(final String url, final ImageView imageView,final Drawable placeholder) { 
     /* Create handler in UI thread. */ 
     final Handler handler = new Handler() { 
      @Override 
      public void handleMessage(Message msg) { 
       String tag = mImageViews.get(imageView); 
       if (tag != null && tag.equals(url)) { 
        if (imageView.isShown()) 
         if (msg.obj != null) { 
          imageView.setImageDrawable((Drawable) msg.obj); 
         } else { 
          imageView.setImageDrawable(placeholder); 
          //Log.d(null, "fail " + url); 
         } 
       } 
      } 
     }; 

     mThreadPool.submit(new Runnable() { 
      public void run() { 
       final Drawable bmp = downloadDrawable(url); 
       // if the view is not visible anymore, the image will be ready for next time in cache 
       if (imageView.isShown()) 
       { 
        Message message = Message.obtain(); 
        message.obj = bmp; 
        //Log.d(null, "Item downloaded: " + url); 

        handler.sendMessage(message); 
       } 
      } 
     }); 
    } 


    /** 
    * Method that download the image 
    * @param url The url image. 
    * @return Returns the drawable associated to this image. 
    */ 
    private Drawable downloadDrawable(String url) { 
     try { 
      InputStream is = getInputStream(url); 

      Drawable drawable = Drawable.createFromStream(is, url); 
      putDrawableInCache(url,drawable); 
      return drawable; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * This method manage the connection to download the image. 
    * @param urlString url of the image. 
    * @return Returns an InputStream associated with the url image. 
    * @throws MalformedURLException 
    * @throws IOException 
    */ 
    private InputStream getInputStream(String urlString) throws MalformedURLException, IOException { 
     URL url = new URL(urlString); 
     URLConnection connection; 
     connection = url.openConnection(); 
     connection.setUseCaches(true); 
     connection.connect(); 
     InputStream response = connection.getInputStream(); 

     return response; 
    } 
    } 

而且,当我用上面的代码来下载我的形象,我注意到,在我的其他活动,在所有影像正在加载,水平滚动(devsmart)不再有效。 当我在加载图像之前尝试滚动时,它仍然有效。所以我试图使用另一个,继this,滚动的作品,但我的图像不能被加载,因为泄漏问题... 任何人都可以分享有关这个问题的想法。任何帮助将不胜感激。

回答

0

所以,我决定使用koush的这个库,UrlImageViewHelper。 这解决了我的问题。图像的加载看起来比我的要稳定。