【问题标题】:Android Path internal StorageAndroid Path 内部存储
【发布时间】:2014-01-20 13:47:34
【问题描述】:

导出数据库的内部路径有问题。日志说这是错误的路径。

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();

日志

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): 在 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-20 08:55:14.827: W/System.err(1383): 在 java.lang.reflect.Method.invoke(Method.java:525) 01-20 08:55:14.827: W/System.err(1383): 在 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): 在 android.os.Handler.handleCallback(Handler.java:730) 01-20 08:55:14.877: W/System.err(1383): 在 android.os.Handler.dispatchMessage(Handler.java:92) 01-20 08:55:14.887: W/System.err(1383): 在 android.os.Looper.loop(Looper.java:137) 01-20 08:55:14.898: W/System.err(1383): 在 android.app.ActivityThread.main(ActivityThread.java:5103) 01-20 08:55:14.927: W/System.err(1383): 在 java.lang.reflect.Method.invokeNative(Native Method) 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): at dalvik.system.NativeStart.main(Native Method) 01-20 08:55:14.987:W/System.err(1383):原因:libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)

【问题讨论】:

  • 请发布您的日志

标签: android sqlite path backup


【解决方案1】:

尝试替换:

File currentDB = new File(data, currentDBPath);

与:

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

硬编码数据库路径不是一个好主意,因为不能保证在所有设备上都相同。使用 getDatabasePath() 将返回创建数据库的路径。所以如果数据库确实存在,就会返回正确的路径。

编辑:

要恢复您之前导出的数据库,请完全按照您对导出所做的操作。唯一的区别是您需要切换源和目标,以便源现在是您的备份文件路径,而目标现在是数据库文件路径。然后数据库将被备份覆盖:

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

【讨论】:

  • 您可以在返回的文件上使用 getParent() 或 getParentFile() 来获取数据库所在的目录。使用 getDatabasePath() 是否修复了您的错误?
  • 是的,getDatabasePath() 没问题。但我想要一些建议或一个从 sd 卡导入的好例子。
  • 请看我的编辑。这是一个反向复制的简单案例。
【解决方案2】:

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

有效路径? 您可以使用 DDMS 签入您的应用数据中存在Backup.db

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多