database - Backup/Restore to sdcard in Android -
i'm trying backup/restore app database external sdcard preferences activity. able save database external sdcard, don't understand how can transfer file default path (data\package.app\databases). idea?
i this:
export:
inputstream myinput; string dbpath = "/data/"+pckgname+"/databases/refuel_db"; string sdpath = environment.getexternalstoragedirectory().getpath(); try { myinput = new fileinputstream(environment.getdatadirectory() + dbpath); // set output folder on scard file directory = new file(sdpath + "/refuel"); // create folder if doesn't exist: if (!directory.exists()) { directory.mkdirs(); } // set output file stream up: outputstream myoutput = new fileoutputstream(directory.getpath() + "/refuel_db"); // transfer bytes input file output file byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close , clear streams myoutput.flush(); myoutput.close(); myinput.close(); toast.maketext(getactivity(), "backup done succesfully!", toast.length_long) .show(); } catch (filenotfoundexception e) { toast.maketext(getactivity(), "error " + e, toast.length_long).show(); // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { toast.maketext(getactivity(), "error " + e, toast.length_long).show(); // todo auto-generated catch block e.printstacktrace(); }
import:
outputstream myoutput; string dbpath = "/data/"+pckgname+"/databases/refuel_db"; string sdpath = environment.getexternalstoragedirectory().getpath(); try { myoutput = new fileoutputstream(environment.getdatadirectory() + dbpath); // set folder on sdcard file directory = new file(sdpath + "/refuel"); // set input file stream up: inputstream myinputs = new fileinputstream(directory.getpath() + "/refuel_db"); // transfer bytes input file output file byte[] buffer = new byte[1024]; int length; while ((length = myinputs.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close , clear streams myoutput.flush(); myoutput.close(); myinputs.close(); toast.maketext(getactivity(), "import done succesfully!", toast.length_long) .show(); } catch (filenotfoundexception e) { toast.maketext(getactivity(), "error " + e, toast.length_long).show(); // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { toast.maketext(getactivity(), "error " + e, toast.length_long).show(); // todo auto-generated catch block e.printstacktrace(); }
Comments
Post a Comment