BinaryContent is an example project demonstrating the use of Android ContentProviders to deliver "large" binary data to a client application.
The applicaion consists of a simple list activity, a content provider and a small service.
The list activity queries the provider for a list of items consisting of an ID and an image. In this case the image is just a random image generated by using the id as a random seed. The IDs used are also random numbers.
The service is a simple IntentService which is used to create the random images asynchonously when they are first needed. The ContentResolver is only notified when all requests are completed to prevent excessive stuttering when scrolling the list.
This class contains the ContentProvider of this application. The
contain the interesting part of the example project. The
query(...) method creates a Cursor
containing three columns:
_idwhich is the standard ID column. This column is also used as text in the list activity.
_datawhich signals the ContentResolver that the
imagecolumn contains a
content://URI that should be used to retrieve large binary data. This column also contains the filename of the file containing the binary data to be read.
content://URI pointing to the same ContentProvider. This URI is passed to
openFile(...)by the ContentResolver.
openFile(Uri, String) method in the ImagesProvider just shows a small debug message and then passes on
the request to the
ContentProvider.openFileHelper(Uri, String) method. The
then queries the ContentProvider using the URI provided and gets back the contents of the
It returns a
ParcelFileDescriptor for the filename in the column.