我想允许我的Android应用程序的用户导出SQLite数据库文件来创建它们的内容。我当前的解决方案将文件复制到私有存储(/data/data/com.package.name/files/Content.db
),然后为该文件创建一个URI并打开共享对话框。这是行得通的,例如,允许我使用Dropbox导出数据库文件。下面是我使用的代码,从https://stackoverflow.com/a/2661882部分调整 -从Android应用程序共享SQLite数据库,无中间副本
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() {
// From 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;
}
}
好像我应该能够直接从现有的数据库路径中创建一个URI,而不是做一个中间副本。有没有办法做到这一点?
我可以继续进行中间复制,但我认为将数据库的第二个副本保留在数据目录中的时间超过必要的时间是个不好的做法。有没有一种方法可以在选择的应用程序使用URI共享文件后将其清除并删除?
在你的应用程序之间共享数据,是你的期望? – 2015-03-31 18:44:16
这听起来像一个非常糟糕的主意。您应该使用Contentproviders。 – wvdz 2015-03-31 18:45:04
如果我只是想让另一个应用程序访问某些数据,ContentProviders会很棒。在这种情况下,我想发送实际的SQLite数据库文件。这主要是为了将数据备份和导出到另一台设备。我最终也可能会选择云解决方案,但我也希望允许用户将自己的数据库文件副本备份到其个人存储上。 – Patrick 2015-04-01 18:30:19