Прежде всего проверьте, что база данных базы данных существует во внутреннем хранилище, если нет, то скопируйте
private void checkDatabase() {
StringBuffer dbPath = new StringBuffer();
File databaseFile;
// Location of the database file, where it will be stored to access
// throughout the program.
dbPath.append("/data/data/");
dbPath.append(getBaseContext().getPackageName());
dbPath.append("/databases/");
// Location of the database file stored in assets folder.
String storedDatabase = Constants.DATABASE_FILE_NAME;
// copy the database
try {
databaseFile = new File(dbPath.toString(), Constants.DATABASE_FILE_NAME);
if (databaseFile.exists()) {
Log.i("database", "database already Exists");
} else {
SQLiteDatabase database = openOrCreateDatabase(Constants.DATABASE_FILE_NAME,
SQLiteDatabase.OPEN_READONLY, null);
database.close();
copyDatasbase(getBaseContext().getAssets(), storedDatabase,
dbPath + Constants.DATABASE_FILE_NAME);
}
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
}
, если базы данных не существует, скопируйте из ресурса во внутреннее хранилище
private void copyDatasbase(AssetManager manager, String sourceFileName,
String destinationFileName) throws IOException {
// Read file from AccessManager
InputStream inputStream = manager.open(sourceFileName);
OutputStream outputStream = new FileOutputStream(destinationFileName);
Log.d("-->", "src: " + sourceFileName);
Log.d("-->", "Des: " + destinationFileName);
byte[] buffer = new byte[3072];
int length;
while ((length = inputStream.read(buffer)) > 0) {
// Write the database file to the folder "databases"
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
outputStream = null;
inputStream = null;
}