Android Tutorial

(avery) #1

By : Ketan Bhimani


372 

import java.io.InputStream;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Handler;
// ...
final String imageSrc = parser.getAttributeValue(null,“href”);
final String currentTitle = new String(title);
imageThread.queueEvent(new Runnable(){
public void run(){
InputStream bmis;
try{
bmis = new URL(imageSrc).openStream();
final Drawable image = new BitmapDrawable(
BitmapFactory.decodeStream(bmis));
mHandler.post(new Runnable(){
public void run(){
imageSwitcher.setImageDrawable(image);
info.setText(currentTitle);
}
});
} catch (Exception e){
Log.e(“Net”, “Failed to grab image”, e);
}
}
});


You can find this block of code within the parser thread, as
previously described. After the image source and title of the image
have been determined, a new Runnable object is queued for
execution on a separate image handling thread. The thread is
merely a queue that receives the anonymous Runnable object
created here and executes it at least 10 seconds after the last one,
resulting in a slideshow of the images from the feed.

As with the first networking example, a new URL object is created
and an InputStream retrieved from it. You need a Drawable object
to assign to the ImageSwitcher. Then you use the
BitmapFactory.decodeStream() method, which takes an
InputStream.
Free download pdf