Skip to content

Instantly share code, notes, and snippets.

@rajiv-singaseni
Created April 12, 2011 11:12
Show Gist options
  • Save rajiv-singaseni/915338 to your computer and use it in GitHub Desktop.
Save rajiv-singaseni/915338 to your computer and use it in GitHub Desktop.
Auto refreshing an image from the server.
class MyActivity extends android.app.Activity {
private ImageView myImageView;
private void refreshImage() {
//send a request to server on a background thread hopefully
new Thread(){
public void run() {
URL aURL = new URL(URL_TO_DOWNLOAD);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 20);
final Bitmap bitMap = BitmapFactory.decodeStream(bis, null, null);
bis.close();
is.close();
MyActivity.this.runOnUIThread(new Runnable() {
myImageView.setImageBitmap(bitMap);
refreshImageComplete();
});
}
}.start();
}
//the function to be called when the image download completes.
private void refreshImageComplete() {
Handler mHandler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
refreshImage();
}
};
//refresh the image for every five seconds
mHandler.postDelayed(r, 5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment