devon
devon

Reputation: 321

Android - ImageLoader won't re-retrieve the image from URL

This is the Activity page named PicURL. I want the app to retrieve the image from the URL whenever this Activity Page is called.

The problem is that this activity page will retrieve the image only once (the first time the activity is called.)

For instance, I open this activity I got picture "A" from the URL. Then I overwrite the picture "A" with picture"B". I reopen this activity but I still got picture "A" which it should be picture"B".

 public class PicURL extends Activity {

    ImageView imageView;

    private ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_url);

        imageLoader = ImageLoader.getInstance();
        imageView = (ImageView) findViewById(R.id.imageView);


        DisplayImageOptions.Builder optionBuilder = new DisplayImageOptions.Builder();
        optionBuilder.showImageForEmptyUri(R.drawable.ic_launcher);
        optionBuilder.showImageOnFail(R.drawable.ic_launcher);
        optionBuilder.cacheInMemory(true);
        optionBuilder.cacheOnDisk(true);
        DisplayImageOptions options = optionBuilder.build();

        ImageLoaderConfiguration.Builder loaderBuilder =
                new ImageLoaderConfiguration.Builder(getApplicationContext());

        loaderBuilder.defaultDisplayImageOptions(options);
        loaderBuilder.diskCacheExtraOptions(400, 400, null);

        ImageLoaderConfiguration config = loaderBuilder.build();

        if (!imageLoader.isInited()) {
            ImageLoader.getInstance().init(config);
        } // Checked if ImageLoader has been initialed or not.


        String imageUri = "http://abcdef.com/pic1.png"; //Where the app retrieve the image from the link
        imageLoader.displayImage(imageUri, imageView);

    }

Apologize for my poor English. Thanks for help!

Upvotes: 1

Views: 875

Answers (1)

nattster
nattster

Reputation: 854

Seem like you have ImageLoader's cache enabled:

optionBuilder.cacheInMemory(true);
optionBuilder.cacheOnDisk(true);

You need to set these options to false. Disable caching will make your app re-load every images from the internet every time, which might be undesirable for you or your users.

You may enable caching, but generate a random query string to the URL that you really want to reload, e.g.,

String randomString = String.format("?random=%d", System.currentTimeMillis());
String imageUri = "http://abcdef.com/pic1.png"+randomString;
imageLoader.displayImage(imageUri, imageView);

Upvotes: 1

Related Questions