android - Save sqlite db in external memory -
i want save sqlite db file in external memory.
i written code save in internal memory below.but want extract "xxx.db" in run time , check whether db fields saved or not.
manifest.xml
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />
and dbhelper.java
private static string db_path = "data/data/com.example.myapp/databases/"; private static final string database_table = "mydb"; private static final int database_version = 1; private static final string database_name = "mydb.db"; public void createdatabase() throws ioexception{ boolean doesdbexist = checkdatabase(); if(doesdbexist){ toast.maketext(ourcontext, "db created", toast.length_short).show(); }else{ this.getreadabledatabase(); try{ copydatabase(); }catch(exception e){ log.d("dberror", "copy db error"); } toast.maketext(ourcontext, "new db created.", toast.length_short).show(); } } private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = ourcontext.getassets().open(database_name); // path created empty db string outfilename = db_path + database_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte buffer[] = new byte[1024]; int len; while ((len = myinput.read(buffer))>0) { myoutput.write(buffer, 0, len); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); }
please me , suggest me required changes.
here's how can save database sd card:
private void backup() throws ioexception { file currentfile = new file(environment.getdatadirectory(), "//data//" + activity.getpackagename() + "//databases//" + database_name); file backupfile = new file(environment.getexternalstoragedirectory(), databasehandler.database_name); backupfile.delete(); backupfile.createnewfile(); fileinputstream fis = new fileinputstream(currentfile); fileoutputstream fos = new fileoutputstream(backupfile); bytestreams.copy(fis, fos); fis.close(); fos.close(); }
note used bytestreams guava.
Comments
Post a Comment