lawrenceagbani
lawrenceagbani

Reputation: 147

Android: Checking if URL has new image data

Here's my situation : - 1. I have a url that contains an image(e.g http://someSite.newImage.png). 2. The url is constant even if I change the image online because I upload new images with same image name(e.g newImage.png), making the url the same but with a different image on each upload. 3. I use the 'InputStream' method to open the url to load latest image in the onCreate method of my activity.

What I want to achieve : - I want my app to be able to detect new image uploads from the url and update my 'ImageView' with latest image while the app is running(currently it only updates my 'ImageView' when the app is first started) and also be able to show maybe a 'ProgressBar' when the image load starts and hide the 'ProgressBar' when the image load ends.

Upvotes: 1

Views: 734

Answers (3)

xyz
xyz

Reputation: 3409

The ideal way would be to to use GCM. However, seeing that the frequency of updates is low,

final int INTERVAL = 5000;
new Timer().scheduleAtFixedRate(
    new TimerTask() {
        @Override
        public void run() {
           boolean modified =true;
           try {

                HttpURLConnection con = (HttpURLConnection) new URL("http://someSite.newImage.png").openConnection();
                con.setRequestMethod("HEAD");
                if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
                    modified = false;
            } catch (Exception e) {
                modified = true;
            }
            if(modified)
                reloadImageAsyncTask.execute();
        }
    }, 
0, INTERVAL);

The above makes periodic requests (polls) the server, and won't fetch the image each time. It should get the job done.

Upvotes: 0

Bajji
Bajji

Reputation: 2393

You might find Google Cloud Messaging (GCM) useful in your case. When you upload a new image, you might as well send a GCM message to all your clients and have the following method implemented client side.

@Override
public void onMessageReceived(String from, Bundle data) {
    // Refetch the image and update it on UI
}

Upvotes: 1

Aakash
Aakash

Reputation: 5261

you can use either gcm or parse.com to implement notifications, in case of gcm you have to send notification from server to device using device ids or if using parse you will have to send notification over all channels or particular channels and all devices will be notified regarding upload.

Use this link for parse.com

or

http://androidexample.com/Android_Push_Notifications_using_Google_Cloud_Messaging_GCM/index.php?view=article_discription&aid=119&aaid=139

or

https://developers.google.com/cloud-messaging/

Upvotes: 1

Related Questions