Share SQLite database from Android app, without intermediate copy -
i want allow users of android app export sqlite database file content create. current solution copies file private storage (/data/data/com.package.name/files/content.db), creates uri file , opens share dialog. working, allowing me export database file using dropbox, example. here code i'm using, partially adapted https://stackoverflow.com/a/2661882 -
private void exportcontent() { copycontenttoprivatestorage(); intent intent = new intent(intent.action_send); intent.settype("application/octet-stream"); uri uri = new fileprovider().getdatabaseuri(this); intent.putextra(intent.extra_stream, uri); intent.setflags(intent.flag_grant_read_uri_permission); startactivity(intent.createchooser(intent, "backup via:")); } private void copycontenttoprivatestorage() { // https://stackoverflow.com/a/2661882 try { file data = environment.getdatadirectory(); file sd = getfilesdir(); if (sd.canwrite()) { string currentdbpath = "//data//com.package.name//databases//content.db"; string backupdbpath = "content.db"; file currentdb = new file(data, currentdbpath); file backupdb = new file(sd, backupdbpath); if (currentdb.exists()) { filechannel src = new fileinputstream(currentdb).getchannel(); filechannel dst = new fileoutputstream(backupdb).getchannel(); dst.transferfrom(src, 0, src.size()); src.close(); dst.close(); } } } catch (exception e) { toast.maketext(this, e.tostring(), toast.length_long).show(); } } public class fileprovider extends android.support.v4.content.fileprovider { public uri getdatabaseuri(context c) { file exportfile = new file(c.getfilesdir(), "content.db"); uri uri = geturiforfile(c, "com.package.name.fileprovider", exportfile); c.granturipermission("*", uri, intent.flag_grant_read_uri_permission); return uri; } } it seems should able directly create uri existing database path, instead of doing intermediate copy. there way this?
i keep doing intermediate copy, believe bad practice leave second copy of database in data directory longer necessary. there way clean , delete after chosen app has finished using uri share file?
share sqlite database using content providers. tutorial can guide more on sqlite database , content provider: android sqlite db , content provider
Comments
Post a Comment