Today I am going to show you how to create an android application which comes with a pre-populated database. The database file has to be stored in assets directory.
Every file that is stored in Assets directory is compressed. Prior to Android 2.3, any compressed asset file with an uncompressed size of over 1 MB cannot be read from the APK. The limit on the uncompressed size of compressed assets was removed in Android 2.3. So someday in the future, when you don’t have to worry about Android versions lower than 2.3, you can avoid this heartache. (source: Brian Hardy-Dealing with Asset Compression in Android Apps
So if you have a database file over 1 mb and you write an application that supports Android versions prior to 2.3 you have 2 options
- Split your database file in chunks which are smaller than 1Mb.
- Ship your file with an extension from the following and it will not be compressed. (This will increase your .apk file size by the size of database file):
1 2 3 4 5 6 7 8 9 10 11//extract from Package.cpp in the aapt source code, on which types of files are not compressed by default: /* these formats are already compressed, or don't compress well */ static const char* kNoCompressExt[] = { ".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg", ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl", ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2", ".amr", ".awb", ".wma", ".wmv" };
Then create your pre-populated database file which has android_metadata table in it and insert a record. (Android creates automatically this table when you are creating your database in a traditional way in SQLiteOpenHelper onCreate() method.) :
|
|
Then create your class which extends SQLiteOpenHelper like this:
|
|