简体   繁体   English

Android Path内部存储

[英]Android Path internal Storage

I'm having a problem with the internal path to export the database. 我在导出数据库的内部路径时遇到问题。 The Log says it is the wrong path. 日志说这是错误的路径。

File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
           FileChannel source=null;
           FileChannel destination=null;
           String cartella = (getString(R.string.app_name));
           String currentDBPath = "/data/"+getPackageName()+"/cartella/"+"Backup.db";
           String backupDBPath = "/cartella/Backup.db";

           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();
                Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
            } catch(IOException e) {
                e.printStackTrace();
                  Toast toast = Toast.makeText(getApplicationContext(),(R.string.Toast_export_errore), Toast.LENGTH_SHORT);
                     toast.show();

Log 日志

01-20 08:55:14.757: W/System.err(1383): java.io.FileNotFoundException: /data/data/info.androidhive.slidingmenu/cartella/Backup.db: open failed: ENOENT (No such file or directory)

01-20 08:55:14.787: W/System.err(1383): at libcore.io.IoBridge.open(IoBridge.java:409) 01-20 08:55:14.787: W/System.err(1383): at java.io.FileInputStream.(FileInputStream.java:78) 01-20 08:55:14.827: W/System.err(1383): at main.Impostazioni.Esporta(Impostazioni.java:334) 01-20 08:55:14.827: W/System.err(1383): at java.lang.reflect.Method.invokeNative(Native Method) 01-20 08:55:14.827: W/System.err(1383): at java.lang.reflect.Method.invoke(Method.java:525) 01-20 08:55:14.827: W/System.err(1383): at android.view.View$1.onClick(View.java:3628) 01-20 08:55:14.857: W/System.err(1383): at android.view.View.performClick(View.java:4240) 01-20 08:55:14.857: W/System.err(1383): at android.view.View$PerformClick.run(View.java:17721) 01-20 08:55:14.877: W/System.err(1383): at android.os.Handler.handleCallback(Handler.java:730) 01-20 08:55:14.877: W/System.err(1383): at android.os.Handler.dispatchMessage(Handler.java:92) 01-20 08:55:14.887: W/System.err(1383): at android.os.Looper.loop(Looper.java: 01-20 08:55:14.787:W / System.err(1383):位于libcore.io.IoBridge.open(IoBridge.java:409)01-20 08:55:14.787:W / System.err(1383) :位于java.io.FileInputStream。(FileInputStream.java:78)01-20 08:55:14.827:W / System.err(1383):位于main.Impostazioni.Esporta(Impostazioni.java:334)01-20 08 :55:14.827:W / System.err(1383):at java.lang.reflect.Method.invokeNative(Native Method)01-20 08:55:14.827:W / System.err(1383):at java.lang .reflect.Method.invoke(Method.java:525)01-20 08:55:14.827:W / System.err(1383):at android.view.View $ 1.onClick(View.java:3628)01-20 08:55:14.857:W / System.err(1383):在android.view.View.performClick(View.java:4240)01-20 08:55:14.857:W / System.err(1383):在Android .view.View $ PerformClick.run(View.java:17721)01-20 08:55:14.877:W / System.err(1383):at android.os.Handler.handleCallback(Handler.java:730)01- 20 08:55:14.877:W / System.err(1383):at android.os.Handler.dispatchMessage(Handler.java:92)01-20 08:55:14.887:W / System.err(1383):at android.os.Looper.loop(Looper.java: 137) 01-20 08:55:14.898: W/System.err(1383): at android.app.ActivityThread.main(ActivityThread.java:5103) 01-20 08:55:14.927: W/System.err(1383): at java.lang.reflect.Method.invokeNative(Native Method) 01-20 08:55:14.927: W/System.err(1383): at java.lang.reflect.Method.invoke(Method.java:525) 01-20 08:55:14.957: W/System.err(1383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 01-20 08:55:14.957: W/System.err(1383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 01-20 08:55:14.987: W/System.err(1383): at dalvik.system.NativeStart.main(Native Method) 01-20 08:55:14.987: W/System.err(1383): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 137)01-20 08:55:14.898:W / System.err(1383):at android.app.ActivityThread.main(ActivityThread.java:5103)01-20 08:55:14.927:W / System.err( 1383):在java.lang.reflect.Method.invokeNative(本地方法)01-20 08:55:14.927:W / System.err(1383):在java.lang.reflect.Method.invoke(Method.java: 525)01-20 08:55:14.957:​​W / System.err(1383):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)01-20 08:55:14.957: W / System.err(1383):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)01-20 08:55:14.987:W / System.err(1383):在dalvik.system .NativeStart.main(本机方法)01-20 08:55:14.987:W / System.err(1383):原因:libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)

Try replacing: 尝试更换:

File currentDB = new File(data, currentDBPath);

with: 有:

File currentDB = getDatabasePath("Backup.db");

Hardcoding the database path is not a good idea since it is not guaranteed to be the same on all devices. 对数据库路径进行硬编码不是一个好主意,因为不能保证在所有设备上都相同。 Using getDatabasePath() will return the path on which the database was created. 使用getDatabasePath()将返回创建数据库的路径。 So if the database does exist, the correct path to it will be returned. 因此,如果数据库确实存在,将返回其正确的路径。

EDIT: 编辑:

To restore a database you have previously exported, do exactly as you did for the export. 要还原以前导出的数据库,请完全按照导出的操作进行。 The only difference is that you need to switch the source and destination around so that the source is now your backup file path and the destination is now the database file path. 唯一的区别是您需要切换源和目标,以便源现在是您的备份文件路径,而目标现在是数据库文件路径。 The database will then be overwritten with the backup: 然后,数据库将被备份覆盖:

source = new FileInputStream(backupDB).getChannel();
destination = new FileOutputStream(currentDB).getChannel();

Is

/data/data/info.androidhive.slidingmenu/cartella/Backup.db

a valid path? 有效路径? You can use DDMS to check in your app data that Backup.db exist 您可以使用DDMS来检查您的应用程序数据中存在Backup.db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM