android - Copy SQlite DB to public directory (non rooted devices) -
i using sqliteopenhelper create database. changed constructor this:
public sqlite(context context){ super(context, "/mnt/sdcard"+database_name+".db", null, database_version); }
the database created in public directory fine. problem when try execute functions, cant change database 1 created in public directory. how can change this? eg:
@override public void oncreate(sqlitedatabase db){ log.i("db path", db.getpath()); }
this prints out:
db path - /data/data/package_name/databases/database_name
i need print out path public database.
thank in advance.
edit
copy private db public directory instead
changed constructor this:
public sqlite(context context){ super(context, database_name, null, database_version); }
used code link geralt suggested:
private void copydatabase(string dbname) throws ioexception { // open local db input stream inputstream myinput = mycontext.getassets().open(dbname); // path created empty db string outfilename = "/data/data/com.sample.view/databases/" + dbname; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); }
here stack trace of happens:
04-15 09:57:55.275: w/resourcetype(11994): invalid package identifier when getting bag resource number 0xffffffff 04-15 09:57:55.275: w/resourcetype(11994): invalid package identifier when getting bag resource number 0xffffffff 04-15 09:57:55.275: w/resourcetype(11994): invalid package identifier when getting bag resource number 0xffffffff 04-15 09:57:55.320: w/system.err(11994): java.io.filenotfoundexception: acollectdatabase 04-15 09:57:55.320: w/system.err(11994): @ android.content.res.assetmanager.openasset(native method) 04-15 09:57:55.320: w/system.err(11994): @ android.content.res.assetmanager.open(assetmanager.java:315) 04-15 09:57:55.320: w/system.err(11994): @ android.content.res.assetmanager.open(assetmanager.java:289) 04-15 09:57:55.320: w/system.err(11994): @ co.za.datasolve.acollect.acollectactivity.copydatabase(acollectactivity.java:184) 04-15 09:57:55.320: w/system.err(11994): @ co.za.datasolve.acollect.acollectactivity.oncreate(acollectactivity.java:153)
and problem line:
inputstream myinput = getapplicationcontext().getassets().open(dbname);
does have suggestions of how can fix this?
i changed constructor this:
problem in constructor you're specifing database name , not path. database created sqliteopenhelper
placed in internal storage security reasons - can't change logic. it's build-in logic os.
all can explicitly copy database directory.
in thread you'll find how copy database.
update:
here how database path:
string path = getapplicationcontext().getdatabasepath("your_database_name");
Comments
Post a Comment