问题
As part of a course on Android app development, I need to write my own ContentProvider. The app I'm making is a simple (? not so sure anymore) photo gallery app, with a master activity showing thumbnails in a GridView, and a detail activity showing one selected image along with some meta data. I've got something working by simply querying the MediaStore.Images.Thumbnails and Images, but -- as I said -- I need + want to learn how to "roll my own" CP.
But! Every tutorial I find deals with interfacing/querying the SQLite database. What I need is to query a directory, a set of files (JPEGs). It really makes NO sense to first dump all photos into the database, only to make the CP-part "easier" (meaning: follow some SQL-CP tutorial).
So: anyone want to help me build a CP for this? Not asking for someone to "do my homework" (btw, this is a free Android course on Udacity -- not school stuff), but I need some advice and pointers in the right direction. I was thinking I'd post what I've got so far, and hopefully someone will comment! The "template" I'm following, is the WeatherContract and WeatherProvider from the Udacity course's "Sunshine" app (which uses SQLite, obviously).
URIs
First: The URIs I want to support. It occurs to me I need two "types": thumbnails and images. For each of these, it should be possible to provide a path to a specific folder, and why not let the user filter on something -- say -- year (DATE_TAKEN). Finally, except for thumbnails, the user can specify a specific image to load in the detail activity. Since this is not a database, we have no "image IDs", so I guess we need to rely on filename here?
Here are the URIs I envision (AUTH is just some authority for my app):
content://AUTH/thumbgives all thumbnails on phone (i.e., equalsMediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI?)content://AUTH/thumb/path/*gives all photos in specified directory (the path will have to be URI-encoded, right, such that/becomes%2F)content://AUTH/thumb/year/#andcontent://AUTH/thumb/path/*/year/#is same as above, but filtered on yearcontent://AUTH/photosgives all photos on phone (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)content://AUTH/photos/year/#content://AUTH/photos/year/#andcontent://AUTH/photos/path/*/year/#- finally,
content://AUTH/photos/path/*/img/*gives a (one) specific image (based on filename, for lack of numerical unique ID)
The "Contract"
With those URIs in mind, what would the PhotoContract look like? I'll post the code I've got so far as a comment below, and update it as I work on it.
The Content Provider
For the implementation (PhotoProvider extends ContentProvider), I guess there are two approaches: Either open a File reader to list *.jpg from the specificed directory/path, dump these into a MatrixCursor, and return. Alternatively, and maybe simpler, would be to simply get a cursor from MediaStore (Thumbnails or Images, depending on URI) and just return that? Why re-invent the wheel?
来源:https://stackoverflow.com/questions/29675268/writing-custom-content-provider-for-photos-on-phone