如何为listview下载图像设置条件?

时间:2013-07-18 10:55:00

标签: android listview download

 public AvatarDownloader(Context context){
    //Make the background thread low priority. This way it will not affect the UI performance
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);

    //Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"/download/myApp/avatars/");
    else
        cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();

}

final int stub_id=R.drawable.ic_launcher;
public void DisplayImage(String url, String profilePic, Activity activity, ImageView imageView)
{
    if(cache.containsKey(url))
        imageView.setImageBitmap(cache.get(url));
    else
    {

        //queuePhoto(url, activity, imageView, profilePic);
        imageView.setImageResource(stub_id);
    }    
}

private void queuePhoto(String url, Activity activity, ImageView imageView, String profilePic)
{
    //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them. 
    photosQueue.Clean(imageView);
    System.err.println("QPH"+url);
    PhotoToLoad p=new PhotoToLoad(url, imageView, profilePic);
    synchronized(photosQueue.photosToLoad){
        photosQueue.photosToLoad.push(p);
        photosQueue.photosToLoad.notifyAll();
    }

    //start thread if it's not started yet
    if(photoLoaderThread.getState()==Thread.State.NEW)
        photoLoaderThread.start();
}

这里我把代码图片下载。如果在服务器ic_launcher图像显示的图像不可用,我想放置代码。如果图像可以在我的列表视图中显示。

1 个答案:

答案 0 :(得分:1)

我建议使用Universal Image Loader进行图片加载/显示相关任务。 它将自动处理内存管理,如果服务器上没有图像,您可以设置自己的图像。