java - is it possible backup and RESTORE a database file in android? non root devices -
this question has answer here:
- backup , restore sqlite database sdcard 4 answers
in app need backup of database, after i'll need restore again,
i have read somethings, not sure if necessary have rooted device, need backup/restore data in non root devices, possible?
my first idea creating txt file write select, , later insert again.
but believe "problem" don't know if possible copy database , paste in sd card backup, , copy sd card , paste in path of database restore non root devices.
here code make work
private void importdb() { try { file sd = environment.getexternalstoragedirectory(); file data = environment.getdatadirectory(); if (sd.canwrite()) { string currentdbpath = "//data//" + "<package name>" + "//databases//" + "<database name>"; string backupdbpath = "<backup db filename>"; // sd directory. file backupdb = new file(data, currentdbpath); file currentdb = new file(sd, backupdbpath); filechannel src = new fileinputstream(backupdb).getchannel(); filechannel dst = new fileoutputstream(currentdb).getchannel(); dst.transferfrom(src, 0, src.size()); src.close(); dst.close(); toast.maketext(getapplicationcontext(), "import successful!", toast.length_short).show(); } } catch (exception e) { toast.maketext(getapplicationcontext(), "import failed!", toast.length_short) .show(); } } private void exportdb() { try { file sd = environment.getexternalstoragedirectory(); file data = environment.getdatadirectory(); if (sd.canwrite()) { string currentdbpath = "//data//" + "<package name>" + "//databases//" + "<db name>"; string backupdbpath = "<destination>"; file currentdb = new file(data, currentdbpath); file backupdb = new file(sd, backupdbpath); filechannel src = new fileinputstream(currentdb).getchannel(); filechannel dst = new fileoutputstream(backupdb).getchannel(); dst.transferfrom(src, 0, src.size()); src.close(); dst.close(); toast.maketext(getapplicationcontext(), "backup successful!", toast.length_short).show(); } } catch (exception e) { toast.maketext(getapplicationcontext(), "backup failed!", toast.length_short) .show(); } }
Comments
Post a Comment