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 query(...)
and openFile(...)
methods
contain the interesting part of the example project. The query(...)
method creates a Cursor
containing three columns:
_id
which is the standard ID column. This column is also used as text in the list activity._data
which signals the ContentResolver that theimage
column contains acontent://
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.image
contains acontent://
URI pointing to the same ContentProvider. This URI is passed toopenFile(...)
by the ContentResolver.
The 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 openFileHelper
method
then queries the ContentProvider using the URI provided and gets back the contents of the _data
column.
It returns a ParcelFileDescriptor
for the filename in the column.