Because @CommonsWare suggested (in the comments to this answer) using the Android SQLiteAssetHelper library, I decided not to use Using your own SQLite database in Android applications method (a popular SO answer) to copy my pre-populated database from my assets folder to the app database folder.
Following the Android SQLiteAssetHelper directions, I set up my project like this:
Since I am using gradle, my database was in src/main/assets/databases/test.db.zip
.
I used the .zip
extension because the directions said
Earlier versions of this library required the database asset to be compressed within a ZIP archive. This is no longer a requirement, but is still supported. Applications still targeting Gingerbread (API 10) or lower should continue to provide a compressed archive to ensure large database files are not corrupted during the packaging process.
and I want to support earlier versions of android.
My database class is similar to the following:
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "test.db.zip";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
Again, I used the .zip
extension for the DATABASE_NAME
value because the directions said
...you must provide...a SQLite database inside the databases folder whose file name matches the database name you provide in code (including the file extension, if any)
However, when I do SQLiteDatabase db = getReadableDatabase();
I get a NullPointerException. What is the problem?
These SO questions and answers are not the same issue: