If you really want to offer structured data or files to other applications, then you’ll have to write your own custom content provider. The entire process of creating your own content provider has various steps involved:
- Create a contract class with all the constants defining URIs, tables, columns, etc. Here’s a nice example of the UserDictionary contract class that defines various constants for content URI, authority, etc. More information on contract class can be found here and here.
-
Create an
SQLiteOpenHelperimplementation that creates/upgrades your database and can be used to access the database from your content provider. -
Finally, create your
ContentProviderimplementation by writing a class that extendsContentProvider. This class will need to implement several methods likequery(),insert(),update(),delete(),getType(),onCreate(). ThegetType()method should return the MIME type corresponding to a content URI (more information here). TheonCreate()method is called right after the provider is created which happens when aContentResolverobject tries to access it. This method can be used to instantiate yourSQLiteOpenHelperimplementation and save a reference to it (instance variable). All the other methods apart from these two are basic CRUD methods. - Add the content provider to the manifest file.
Although I was about to explain the entire process, but then I realized that the documentation does a good job (easy to understand) and is pretty comprehensive too. So here are a few links that you should definitely read to completely get a hang of how to create your own custom content provider:
- https://developer.android.com/guide/topics/providers/content-provider-creating.html
- http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
- The thread safety block here is a very important piece of information to keep in mind.
Hope that helps!