2
 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 图像显示图像不可用,我想输入代码。如果图像可用,则在我的列表视图中显示。

4

1 回答 1

1

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

于 2013-07-18T11:05:26.970 回答