Android copy database directory issue? -
i have application need access assets folder database data when application start, doing copy data assets action, in last release, user cannot open application, user can.
may know, if need copy database assets folder user device, database directory path should use store copied database? or missing important thing in code?
as now, doing on device:
createdirectory:
public boolean createdirectory() { if(environment.getexternalstoragestate().contains(environment.media_mounted)) { string rootpath = environment.getexternalstoragedirectory().getabsolutepath()+globalconfig.config_data_dir; if( isneeddl( rootpath )) { file mydir = new file(rootpath); return mydir.mkdirs(); } } return false; }
and database directory, globalconfig.config_data_dir = "/data/"
path = environment.getexternalstoragedirectory().getabsolutepath()+globalconfig.config_data_dir;
and in sqliteopenhelper class: check if database exited, copy action.
private noldatabasehelper(context context, string path) { super(context, path + db_name_outdbname, null, db_version); this.context = context; } public void createdatabase() { if(!isdatabaseexited()) { log.v(globalconfig.tag, "no database"); getreadabledatabase(); copydatabasefromasset(); } else { log.v(globalconfig.tag, "has database"); } } public static boolean isdatabaseexited() { sqlitedatabase checkdb = null; try { checkdb = sqlitedatabase.opendatabase( db_path + db_name_outdbname, null, sqlitedatabase.open_readonly); checkdb.close(); log.v(globalconfig.tag,"check? "+db_path + db_name_outdbname+" checkdb:"+checkdb); } catch (sqliteexception e) {} return checkdb != null ? true : false; }
thanks!
here copy function:
// fix database locked issue private synchronized void copydatabasefromasset() { int length = -1; byte[] buffer = new byte[1024]; assetmanager = context.getassets(); try { inputstream assetsdb = am.open(db_name_indbname); string outfilename = db_path + db_name_outdbname; outputstream dbout = new fileoutputstream(outfilename); while ((length = assetsdb.read(buffer)) > 0) { dbout.write(buffer, 0, length); } dbout.flush(); dbout.close(); assetsdb.close(); } catch(exception e) { log.e( globalconfig.tag, "copydatabasefromasset error, say: "+e.tostring() ); } }
below working code copying database assets folder data directory:
public class databasehelper extends sqliteopenhelper { // varaible declaration public static string tag = "databasehelper"; private static string db_name = "dmdatabase.sqlite";//file name in assets private sqlitedatabase sqlitedatabase; private final context mycontext; private string databasepath; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; databasepath = "/data/data/" + mycontext.getpackagename() + "/databases/"; } /** * method create database inside application * * @throws ioexception */ public void createdatabase() throws ioexception { try { // check if database exists boolean dbexist = checkdatabase(); if (!dbexist) { // database not present copy databse this.getreadabledatabase(); try { copydatabse(); } catch (ioexception e) { // todo: handle exception // string ex = e.getmessage(); e.printstacktrace(); } } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } /** * check if database exist avoid re-copying file each * time open application. * * @return true if exists, false if doesn't */ private boolean checkdatabase() { sqlitedatabase checkdb = null; try { checkdb = null; try { file file = new file(databasepath + db_name); if (file.exists()) { checkdb = sqlitedatabase.opendatabase(databasepath + db_name, null, sqlitedatabase.open_readonly); } else { return false; } } catch (sqlexception e) { log.d("db exception", ""); } if (checkdb != null) { checkdb.close(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return checkdb != null ? true : false; } /** * copies database local assets-folder created * empty database in system folder, can accessed , * handled. done tranfering bytestream. * */ private void copydatabse() throws ioexception { try { // open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = databasepath + db_name; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024 * 2]; int length; while ((length = myinput.read(buffer)) > 0) { try { myoutput.write(buffer, 0, length); } catch (exception e) { } } myoutput.flush(); myoutput.close(); myinput.close(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub createdatabase(); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } /** * other methods insert,delete, update,select in database */ }
i hope helpful you.
but not recommended process copy database assets data directory. once database on created,
oncreate
method called in should use queries create tables , insert default data database.