Created
April 12, 2011 11:12
-
-
Save rajiv-singaseni/915338 to your computer and use it in GitHub Desktop.
Auto refreshing an image from the server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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