1

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.

lawrenceagbani
  • 147
  • 1
  • 11

3 Answers3

1

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
}
Bajji
  • 2,243
  • 2
  • 22
  • 35
  • I've never used google cloud messaging and I upload my images to Windows Azure blob storage. – lawrenceagbani Jul 24 '15 at 18:22
  • Do you upload the images manually or using any automated script ? – Bajji Jul 24 '15 at 18:24
  • So whenever you push the new set of images, you can manually trigger a script which sends the notification to your clients' devices. Your app can handle this notification and update the image. You can look at https://www.digitalocean.com/community/tutorials/how-to-create-a-server-to-send-push-notifications-with-gcm-to-android-devices-using-python for some idea – Bajji Jul 24 '15 at 18:33
1

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/

Aakash
  • 5,181
  • 5
  • 20
  • 37
0

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.

xyz
  • 3,349
  • 1
  • 23
  • 29
  • so it basically fetches the image if `modified` is `true`, right? @prakharsingh95 – lawrenceagbani Jul 24 '15 at 18:35
  • I tried your code @prakharsingh95, it's not working. I placed a log at the first `if ` statement but the log doesn't show up in log cat, I placed a log at the beginning of the method in which I put the code and found that the method is being called. Please help me check it out – lawrenceagbani Jul 24 '15 at 19:34
  • @bigdee94 I updated it. Its logically correct but there might me a minor syntax error, and I am not at my laptop so I can't test it right now. – xyz Jul 24 '15 at 19:40