Writing custom Content Provider for photos on phone

廉价感情. 提交于 2019-12-11 11:29:03

问题


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):

  1. content://AUTH/thumb gives all thumbnails on phone (i.e., equals MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI ?)
  2. content://AUTH/thumb/path/* gives all photos in specified directory (the path will have to be URI-encoded, right, such that / becomes %2F)
  3. content://AUTH/thumb/year/# and content://AUTH/thumb/path/*/year/# is same as above, but filtered on year
  4. content://AUTH/photos gives all photos on phone (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
  5. content://AUTH/photos/year/#
  6. content://AUTH/photos/year/# and content://AUTH/photos/path/*/year/#
  7. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!