Android -imageview显示从URL下载的旧图像

时间:2015-03-05 08:23:04

标签: android

这是我正在使用的代码。

链接包含图片 http://mycapturetest.byethost17.com/Screen.jpg

当我在URl上更新图像并在浏览器上打开它时。它显示更新的图像。

但是当在Android应用程序中打开相同的链接时,它会继续显示上传到URl上的最后一个图像。 (不是新的)

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;



    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
        bmImage.setImageDrawable(null);
        bmImage.setImageBitmap(null);
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

               bmImage.setImageDrawable(null);
               bmImage.setImageBitmap(null);
               bmImage.setImageBitmap(result);
               Toast.makeText(MainActivity.this, "set....", Toast.LENGTH_SHORT).show();


    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用URLConnection类关闭从URL加载的资源的缓存:

    try {
        URLConnection connection = new java.net.URL(urldisplay).openConnection();
        connection.setUseCaches(false);
        InputStream in = connection.getInputStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }