我希望允许我的 Android 应用程序的用户为他们创建的内容导出 SQLite 数据库文件。我当前的解决方案将文件复制到私有存储 ( /data/data/com.package.name/files/Content.db),然后为此文件创建一个 URI 并打开共享对话框。这是有效的,例如,允许我使用 Dropbox 导出数据库文件。这是我正在使用的代码,部分改编自/sf/answers/186331771/ -
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 /sf/answers/186331771/
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()) { …Run Code Online (Sandbox Code Playgroud)