Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
Find and Display an Image

Listing 33.23  Downloading and displaying image (LocatrFragment.java)


private class SearchTask extends AsyncTask<Location,Void,Void> {
private GalleryItem mGalleryItem;
private Bitmap mBitmap;


@Override
protected Void doInBackground(Location... params) {
...
mGalleryItem = items.get(0);


try {
byte[] bytes = fetchr.getUrlBytes(mGalleryItem.getUrl());
mBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
} catch (IOException ioe) {
Log.i(TAG, "Unable to download bitmap", ioe);
}
return null;
}


@Override
protected void onPostExecute(Void result) {
mImageView.setImageBitmap(mBitmap);
}
}


With that, you should be able to find a nearby image on Flickr (Figure 33.11). Fire up Locatr and press
your location button.


Figure 33.11  The final product

Free download pdf