Android:检查URL是否有新的图像数据

时间:2015-07-24 18:03:52

标签: android image url

这是我的情况: - 1.我有一个包含图像的网址(例如http://someSite.newImage.png)。 2.即使我在线更改图像,网址也是不变的,因为我上传了具有相同图像名称的新图像(例如newImage.png),使网址相同但每次上传时图像不同。 3.我使用' InputStream'打开url以在我的活动的onCreate方法中加载最新图像的方法。

我想要达到的目标: - 我希望我的应用能够从网址检测到新的图片上传,并更新我的“ImageView”图像。在应用程序运行时显示最新图像(目前它只更新我的' ImageView'当应用程序首次启动时),并且还能够显示可能的“ProgressBar”#39;当图像加载开始并隐藏'ProgressBar'当图像加载结束时。

3 个答案:

答案 0 :(得分:1)

您可能会发现Google Cloud Messaging (GCM)对您的案例很有用。上传新图像时,您也可以向所有客户端发送GCM消息,并在客户端实现以下方法。

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

答案 1 :(得分:1)

您可以使用gcm或parse.com来实现通知,如果是gcm,您必须使用设备ID从服务器向设备发送通知,或者如果使用解析,则必须通过所有渠道或特定渠道发送通知设备将收到有关上传的通知。

Use this link for parse.com

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

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

答案 2 :(得分:0)

理想的方法是使用GCM。但是,看到更新的频率很低,

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

以上内容定期请求(轮询)服务器,并且每次都不会获取图像。它应该完成工作。

相关问题