我有一个数据库,我想将其复制到SD卡以保留备份,我找到了这个代码:
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.example.myapp" +"/databases/"+"mydatabase";
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
//e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
正如我在标题中写的那样,当我尝试这样做时,我得到No such file or directory
ENOENT错误。
显然数据库应该存储在那里。 我试过改变一些不同的东西,但仍然没有...... 我试过了:
"/data/"+ "com.example.myapp" +"/"+"mydatabase.db"
和
"//data//"+ "com.example.myapp" +"//databases//"+"mydatabase"
Edit1:也尝试了这个并且也不起作用:
String currentDBPath = this.getDatabasePath("mydatabase").toString();
等。什么都行不通,我也找不到以任何方式检查路径的方法。
我基本上已经创建了数据库,在其中插入了几行并具有一些基本功能,如add()
和delete()
等。我错过了什么?我不知道还能尝试什么。
以下是logcat对此的评价:
01-18 11:34:46.215 10337-10337 / com.example.myapp D / DBPATH:数据库路径为/data/data/com.example.myapp/databases/mydatabase.db
01-18 11:34:46.225 10337-10337 / com.example.myapp W / System.err:java.io.FileNotFoundException:/data/data/com.example.myapp/databases/mydatabase.db:open失败:ENOENT(没有这样的文件或目录)
01-18 11:34:46.235 10337-10337 / com.example.myapp W / System.err:at libcore.io.IoBridge.open(IoBridge.java:456)
01-18 11:34:46.235 10337-10337 / com.example.myapp W / System.err:at java.io.FileInputStream。(FileInputStream.java:76)
01-18 11:34:46.235 10337-10337 / com.example.myapp W / System.err:at com.example.myapp.TabsActivity.exportDB(TabsActivity.java:183)
01-18 11:34:46.235 10337-10337 / com.example.myapp W / System.err:at com.example.myapp.TabsActivity.onOptionsItemSelected(TabsActivity.java:119)
答案 0 :(得分:1)
如下所示,问题可能与数据库无关,而是可能与外部存储有关,因为目录不存在。修复是创建目录。希望以下内容可行(对我而言): -
try {
source = new FileInputStream(currentDB).getChannel();
(new File(backupDB.getParent())).mkdirs(); //<<<<<<<<<<<<<< ADDED
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
这可能有用Storage permission error in Marshmallow
获取代码并根据其进行调整(检查路径,参见输出): -
java.io.FileNotFoundException: / mnt / sdcard / storage / extSdCard / mydatabase:open failed:ENOENT(不是这样的 文件或目录)
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ getPackageName() +"/databases/"+DatabaseHandler.DATABASE_NAME;
String retreivedDBPAth = getDatabasePath(DatabaseHandler.DATABASE_NAME).getPath();
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
File retrievedDB = new File(retreivedDBPAth);
Log.d("PATHS", " CurrentDB=" +
currentDBPath + "\n\t" + currentDB.getPath() +
"\n\tExists=" + String.valueOf(currentDB.exists()) +
"\nBackup=" + backupDBPath + "\n\t" + backupDB.getPath() +
"\n\tExists=" + String.valueOf(backupDB.exists()) +
"\nRetrieved DB=" + retreivedDBPAth + "\n\t" + retrievedDB.getPath() +
"\n\tExists=" + String.valueOf(retrievedDB.exists())
);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
ENOENT不适用于数据库,但适用于外部存储: -
01-18 11:11:32.017 2689-2689/? D/PATHS: CurrentDB=/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
Backup=/storage/extSdCard/mydatabase
/mnt/sdcard/storage/extSdCard/mydatabase
Exists=false
Retrieved DB=/data/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
01-18 11:11:32.021 2689-2689/? W/System.err: java.io.FileNotFoundException: /mnt/sdcard/storage/extSdCard/mydatabase: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:416)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.exportDB(MainActivity.java:162)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.onCreate(MainActivity.java:38)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Activity.performCreate(Activity.java:5008)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Looper.loop(Looper.java:137)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 11:11:32.021 2689-2689/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.Posix.open(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:400)
01-18 11:11:32.021 2689-2689/? W/System.err: ... 18 more