Android-使用url加载图像,但加载后,另一个图像将替换另一个图像

Android-使用url加载图像,但加载后,另一个图像将替换另一个图像,android,android-image,Android,Android Image,我想知道这是怎么回事。我的应用程序使用url加载图像。 我已经成功地完成了加载和显示。问题是,, 图像显示时,显示错误的图像。。。 最好在图像中显示: 首先,当其他图像仍在加载时,将显示正确的图像[顶部图像]。 但在加载所有图像后,img2复制并替换img1,但仍使用 img1的尺寸 这是我加载图像时使用的代码: public class DrawableBackgroundDownloader { private final Map<String, Drawable&

我想知道这是怎么回事。我的应用程序使用url加载图像。 我已经成功地完成了加载和显示。问题是,, 图像显示时,显示错误的图像。。。 最好在图像中显示:

首先,当其他图像仍在加载时,将显示正确的图像[顶部图像]。 但在加载所有图像后,
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;
  }
  }
公共类DrawableBackgroundDownloader{
私有最终映射mCache=newhashmap();
private final LinkedList mChacheController=new LinkedList();
私有执行器服务线程池;
私有最终映射mImageViews=Collections.synchronizedMap(新的WeakHashMap());
公共静态int MAX_CACHE_SIZE=80;
公共int线程池大小=3;
/**
*建造师
*/
public DrawableBackgroundDownloader(){
mThreadPool=Executors.newFixedThreadPool(线程池大小);
}  
/**
*清除所有实例数据并停止运行线程
*/ 
公共无效重置(){
ExecutorService oldThreadPool=MTThreadPool;
mThreadPool=Executors.newFixedThreadPool(线程池大小);
oldThreadPool.shutdownNow();
mChacheController.clear();
mCache.clear();
mImageViews.clear();
}  
/**
*加载与url关联的绘图表并将其分配给图像,您可以设置占位符来替换此绘图表。
*@param url是图像的url。
*@param imageView要分配可绘制图像的图像。
*@param占位符在图像下载过程中显示的可绘制文件。
*/
public void loadDrawable(最终字符串url、最终图像视图、可绘制占位符){
如果(!mImageViews.containsKey(url))
mImageViews.put(图像视图,url);
Drawable Drawable=getDrawableFromCache(url);
//签入UI线程,因此没有并发问题
如果(可绘制!=null){
//Log.d(null,“从McCache加载的项:“+url”);
imageView.setImageDrawable(可绘制);
}否则{
imageView.setImageDrawable(占位符);
queueJob(url、imageView、占位符);
}  
} 
/**
*从缓存中返回一个可绘制文件。
*@param图像的url。
*@如果缓存中存在图像,则返回可绘制的,否则返回null。
*/
公共可绘制getDrawableFromCache(字符串url){
if(mCache.containsKey(url)){
返回mCache.get(url);
}  
返回null;
}
/**
*将图像保存到缓存中。
*@param url图像url
*@param drawable可提取文件保存。
*/
私有同步的void putDrawableInCache(字符串url,Drawable Drawable){
int chachecontrollerise=mChacheController.size();
如果(chacheControllerSize>最大缓存大小)
mChacheController.subList(0,最大缓存大小/2).clear();
mChacheController.addLast(可绘制);
mCache.put(url,可绘制);
}  
/**
*将作业排队以下载映像。
*@param url图像url。
*@param imageView分配了可绘制图形的imageView。
*@param占位符图像下载过程中显示的可绘制文件。
*/
私有void queueJob(最终字符串url、最终图像视图、最终可绘制占位符){
/*在UI线程中创建处理程序*/
最终处理程序=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
String tag=mImageViews.get(imageView);
if(tag!=null&&tag.equals(url)){
if(imageView.isShown())
如果(msg.obj!=null){
setImageDrawable((Drawable)msg.obj);
}否则{
imageView.setImageDrawable(占位符);
//Log.d(空,“失败”+url);
} 
}  
}  
};  
mThreadPool.submit(新的Runnable(){
public void run(){
最终可绘制bmp=可下载可绘制(url);
//如果视图不再可见,则图像将准备好下次在缓存中使用
if(imageView.isShown())
{
Message=Message.get();
message.obj=bmp;
//Log.d(空,“下载项目:”+url);
handler.sendMessage(message);
}
}  
});  
}  
/**
*方法下载图像
*@param url返回url图像。
*@return返回与此图像关联的可绘制文件。
*/
私有可提取可下载可提取(字符串url){
试试{
InputStream is=getInputStream(url);
Drawable Drawable=Drawable.createFromStream(is,url);
putDrawableInCache(url,可绘制);
回拉;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE){
e、 printStackTrace();
}  
返回null;
}  
/**
*此方法管理下载映像的连接。
*@param urlString图像的url。
*@return返回与url图像关联的InputStream。
*@抛出错误的DurException
*@抛出异常
*/
私有InputStream getInputStream(字符串urlString)引发畸形的异常,IOException{
URL=新URL(URL字符串);
URL连接;
connection=url.openConnection();
connection.setUseCaches(true);
connection.connect();
InputStream响应=connection.getInputStream();
返回响应;
}
}
另外,当我使用上面的代码下载我的图片时,我注意到,在我的其他活动中,aft