如何在单击按钮时从 URL 刷新图像视图加载



如何在单击按钮时从URL重新加载图像视图?

我尝试的是,在主要活动中

makeImageRequest();
btnImageReq.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         makeImageRequest();
    }
});

和我的自定义方法来加载图像,

private void makeImageRequest() {
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    imageLoader
            .get("http://api.androidhive.info/volley/volley-image.jpg",
                    ImageLoader.getImageListener(imageView,
                            R.drawable.ico_loading, R.drawable.ico_error));
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Entry entry = cache
            .get("http://api.androidhive.info/volley/volley-image.jpg");
    if (entry != null) {
        try {
            String data = new String(entry.data, "UTF-8");
            // handle data, like converting it to xml, json, bitmap etc.,
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else {
    }

但是在单击按钮时,图像视图不会再次加载。

尝试使用 AQuery 在异步中加载图像并允许图像缓存选项:

AQuery aq = new AQuery(context);

不允许图像缓存:

aq.id(imageView).progress(R.drawable.ico_loading).image("http://api.androidhive.info/volley/volley-image.jpg",false,false,0,R.drawable.ico_error);

允许文件和内存映像缓存:

aq.id(imageView).progress(R.drawable.ico_loading).image("http://api.androidhive.info/volley/volley-image.jpg",true,true,0,R.drawable.ico_error);

请在刷新图像之前清除图像视图。

在图像视图中设置新图像之前放置以下代码

//Add this line
  imageView.setBackground(null);

例如

private void makeImageRequest() {
   //Add this line
    imageView.setBackground(null);
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
imageLoader
            .get("http://api.androidhive.info/volley/volley-image.jpg",
                    ImageLoader.getImageListener(imageView,
                            R.drawable.ico_loading, R.drawable.ico_error));
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Entry entry = cache
            .get("http://api.androidhive.info/volley/volley-image.jpg");
    if (entry != null) {
        try {
            String data = new String(entry.data, "UTF-8");
            // handle data, like converting it to xml, json, bitmap etc.,
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else {
            }

最新更新