如何刷新按钮点击URL上的图像视图加载?

时间:2014-12-15 06:03:26

标签: android

如何在按钮点击时从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 {
    }

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

2 个答案:

答案 0 :(得分:0)

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

在Imageview

中设置新图像之前,请放置以下代码
//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 {
            }

答案 1 :(得分:0)

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

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);