Add permissions in Manifest first
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
Then add the method in mainActivity
SQLiteDatabase db;
Private final String DATABASE_PATH = android. OS. Environment
. GetExternalStorageDirectory (). getAbsolutePath () + "/vote ";
Private String DATABASE_FILENAME = "db_vote.db ";
// Initialize the database
Private SQLiteDatabase openDatabase (){
Try {
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File (DATABASE_PATH );
If (! Dir. exists ())
Dir. mkdir ();
If (! (New File (databaseFilename). exists ()){
InputStream is = getResources (). openRawResource (R. raw. db_vote );
FileOutputStream fos = new FileOutputStream (databaseFilename );
Byte [] buffer = new byte [8192];
Int count = 0;
While (count = is. read (buffer)> 0 ){
Fos. write (buffer, 0, count );
}
Fos. close ();
Is. close ();
}
Db = SQLiteDatabase. openOrCreateDatabase (databaseFilename, null );
Return db;
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}
From oldfeel