Android Tutorial

(avery) #1

By : Ketan Bhimani


344 

Retrieving Content Provider Data with managedQuery()

We can query the Media Store content provider using the URI much
like we would query a database. We now use the managedQuery()
method to return a managed Cursor containing all image media
available on the SD card.

String[] projection = new String[] { Media._ID, Media.TITLE };
Uri mMedia = Media.EXTERNAL_CONTENT_URI;
Cursor mCursorImages = managedQuery(mMedia, projection, null, null,
Media.DATE_TAKEN + “ ASC”); // Order-by clause.


We have retrieved the records for each piece of media available on
the SD card.

Now we have this Cursor, but we still have some legwork to get our
Gallery widget to display the individual images.

Data-Binding to the Gallery Control

We need to extend the BaseAdapter class for a new type of data
adapter called ImageUriAdapter to map the URI data we retrieved
to the Gallery widget. Our custom Image UriAdapter maps the
Cursor results to an array of GalleryRecord objects, which
correspond to the child items within the Gallery widget. Although
the code for the ImageUriAdapter is too long to show here, we go
over some of the methods you must implement for the adapter to
work properly.

 The ImageUriAdapter() constructor is responsible for mapping the Cursor
to an array of GalleryRecord objects, which encapsulate the base URI and
the individual image’s id. The image id is tacked on to the end of the URI,
resulting in a fully qualified URI for the individual image.
 The getItem() and getItemId() methods return the unique identifier for the
specific image. This is the value we require when the user clicks on a
Free download pdf