2014-01-20 168 views
0

我遇到了導出數據庫的內部路徑問題。日誌說這是錯誤的路徑。Android路徑內部存儲

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) 

八月1日至20日:55:14.787:W/System.err的(1383):在libcore.io.IoBridge.open(IoBridge.java:409) 01 - 08:55:14.827:W/System.err(1383):在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):在java.lang.reflect.Met hod.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: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.M ethod.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法) 八月1日至20日:55:14.987:W/System.err的(1383):libcore.io.ErrnoException:致打開失敗:ENOENT(沒有這樣的文件或目錄)

+0

請張貼您的日誌 –

回答

1

嘗試更換:

File currentDB = new File(data, currentDBPath); 

有:

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

硬編碼的數據庫路徑是不是一個好主意,因爲它是不保證在所有設備上都一樣。使用getDatabasePath()將返回創建數據庫的路徑。因此,如果數據庫確實存在,則返回正確的路徑。

編輯:

要恢復您先前導出的數據庫,做完全像你一樣的出口。唯一的區別是您需要切換源和目標,以便源現在成爲備份文件路徑,而目標現在是數據庫文件路徑。然後數據庫將被覆蓋備份:

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

好的謝謝,並從同一目錄中導入? – user3160725

+0

您可以在返回的File上使用getParent()或getParentFile()來獲取數據庫所在的目錄。使用getDatabasePath()修復了您的錯誤嗎? – NigelK

+0

是的,getDatabasePath()很好。但我想要一些建議或一個很好的例子從SD卡導入。 – user3160725

0

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

一個有效的路徑? 您可以使用DDMS在你的應用數據,從而檢查Backup.db存在