【发布时间】:2019-11-12 13:05:37
【问题描述】:
晚上好,我正在开发一个 android 应用程序,它有 SQLite 数据库,我需要一种方法将数据库复制到设备的外部存储,在那里我可以复制到另一个设备,因此我可以将数据库导入到另一台设备。
例如:
假设应用程序调用“example”,数据库在“/data/data/com.gnd.example/databases”文件夹中,名为data.db,需要复制到“example/backup”文件夹”,例如“/storage/emulated/0/Example/Backup”。这是第一部分。
第二部分是导入,应用程序应该将文件从“example / import”文件夹复制到文件夹“/data/data/com.gnd.example/databases”
为此,我有两个按钮活动,btn_export 和 btn_import。
我已经依赖以下解决方案:
导入/导出到 android sqlite 数据库 Android上SQLite数据库的简单导出和导入
我已经在 AndroidManifest 中插入了它
如何向用户请求权限?
我尝试使用我在其中一个示例中获取的这段代码进行复制
private void backupDatabase () throws IOException {
String inFileName = "/data/data/com.gnd.example/databases/dados.db";
File dbFile = new File (inFileName);
FileInputStream fis = new FileInputStream (dbFile);
String outFileName = Environment.getExternalStorageDirectory () + "/ example / backup / data.db";
OutputStream output = new FileOutputStream (outFileName);
byte [] buffer = new byte [1024];
int length;
while ((length = fis.read (buffer))> 0) {
output.write (buffer, 0, length);
}
output.flush ();
output.close ();
fis.close ();
}
按钮如下所示:
@Override
public void onClick (View view) {
try {
backupDatabase ();
} catch (IOException e1) {
e1.printStackTrace ();
}
});
按下按钮时记录:
07/01 19:35:39: Launching app
$ adb shell am start -n "com.gnd.keepkey / com.gnd.keepkey.Telephone" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 27724 on device motorola-moto_z2_play-0039635857
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I / zygote: Do partial code cache collection, code = 20KB, data = 29KB
I / zygote: After code cache collection, code = 20KB, data = 29KB
Increasing code cache capacity to 128KB
I / zygote: Do partial code cache collection, code = 20KB, data = 47KB
I / zygote: After code cache collection, code = 20KB, data = 47KB
Increasing code cache capacity to 256KB
I / zygote: Compiler allocated 4MB to compile void android.widget.TextView. <Init> (android.content.Context, android.util.AttributeSet, int, int)
I / zygote: Full code cache collection, code = 120KB, data = 82KB
I / zygote: After code cache collection, code = 117KB, data = 62KB
I / zygote: Do partial code cache collection, code = 125KB, date = 79KB
I / zygote: After code cache collection, code = 125KB, data = 79KB
Increasing code cache capacity to 512KB
W / System.err: java.io.FileNotFoundException: /storage/emulated/0/teste/dados.db (No such file or directory)
at java.io.FileOutputStream.open0 (Native Method)
W / System.err: at java.io.FileOutputStream.open (FileOutputStream.java:287)
at java.io.FileOutputStream. <init> (FileOutputStream.java:223)
at java.io.FileOutputStream. <init> (FileOutputStream.java:110)
at com.gnd.keepkey.funcoes.Exportar_Importar.backupDatabase (Export_Importar.java:87)
at com.gnd.keepkey.funcoes.Exportar_Importar.access $ 000 (Export_Importar.java:42)
at com.gnd.keepkey.funcoes.Export_Import $ 1.onClick (Export_Import.java:69)
W / System.err: at android.view.View.performClick (View.java:6259)
at android.view.View $ PerformClick.run (View.java:24732)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6592)
at java.lang.reflect.Method.invoke (Native Method)
W / System.err: at com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:769)
【问题讨论】:
-
“因为不是所有的设备都有一个”——每个 Android 设备都有 Android SDK 所指的external storage。 “我可以将它复制到另一台设备,因此您可以将数据库导入另一台设备的应用程序”——用户无权访问 Android SDK 所指的internal storage。除此之外,尚不清楚您的问题是什么。您可能想添加 minimal reproducible example 并解释发生了什么问题。
-
这样更好吗?抱歉英文有误,我正在使用翻译,我是这个存储问题的初学者,所以这个困难和这些关于内部和外部的错误。
标签: java android database sqlite