2017-10-07 197 views
-2

我在我的應用程序中有一個數據庫。我想將其從我的應用程序內部存儲備份或導出到外部(共享)存儲。我有以下代碼來實現目標。如何將數據庫從內部存儲備份到外部(共享)存儲

public void exportDB(){ 

    File sd = Environment.getExternalStorageDirectory(); 
    File data = Environment.getDataDirectory(); 

    FileChannel source = null; 
    FileChannel destination = null; 

    File dbFile = context.getDatabasePath(SAMPLE_DB_NAME); 

    String currentDBPath = dbFile.toString(); 
    String backupDBPath = SAMPLE_DB_NAME; 

    File currentDB = new File(data, currentDBPath); 
    File backupDB = new File(sd, backupDBPath); 

    if(sd.canWrite() && sd.canRead()) 
     Toast.makeText(context, "sd can read and write", Toast.LENGTH_LONG).show(); 
    else if(sd.canWrite() && !sd.canRead()) 
     Toast.makeText(context, "sd cannot read but write", Toast.LENGTH_LONG).show(); 
    else if(!sd.canWrite() && sd.canRead()) 
     Toast.makeText(context, "sd can read but not write", Toast.LENGTH_LONG).show(); 
    else 
     Toast.makeText(context, "sd cannot read and write", Toast.LENGTH_LONG).show(); 


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

我已經在清單

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

問題添加這兩種權限是我得到的是SD卡的選項錯誤是不可寫,而不是可讀從我的if else語句。

其次我在日誌中得到這個錯誤。

10-07 11:58:25.388 18913-18913/com.free W/System.err: java.io.FileNotFoundException: /data/data/user/0/com.free/databases/database.db (No such file or directory) 
10-07 11:58:25.390 18913-18913/com.free W/System.err:  at java.io.FileInputStream.open(Native Method) 
10-07 11:58:25.392 18913-18913/com.free W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:146) 
10-07 11:58:25.394 18913-18913/com.free W/System.err:  at com.free.DatabaseManage.exportDB(DatabaseManage.java:56) 
10-07 11:58:25.396 18913-18913/com.free W/System.err:  at com.free.MainActivity.onNavigationItemSelected(MainActivity.java:773) 
10-07 11:58:25.398 18913-18913/com.free W/System.err:  at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:156) 
10-07 11:58:25.400 18913-18913/com.khan.free W/System.err:  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) 
10-07 11:58:25.402 18913-18913/com.free W/System.err:  at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:88) 
10-07 11:58:25.403 18913-18913/com.free W/System.err:  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156) 
10-07 11:58:25.405 18913-18913/com.free W/System.err:  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969) 
10-07 11:58:25.407 18913-18913/com.free W/System.err:  at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342) 
10-07 11:58:25.409 18913-18913/com.free W/System.err:  at android.view.View.performClick(View.java:6213) 
10-07 11:58:25.411 18913-18913/com.free W/System.err:  at android.view.View$PerformClick.run(View.java:23645) 
10-07 11:58:25.413 18913-18913/com.free W/System.err:  at android.os.Handler.handleCallback(Handler.java:751) 
10-07 11:58:25.418 18913-18913/com.free W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
10-07 11:58:25.419 18913-18913/com.free W/System.err:  at android.os.Looper.loop(Looper.java:154) 
10-07 11:58:25.420 18913-18913/com.free W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6692) 
10-07 11:58:25.421 18913-18913/com.free W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
10-07 11:58:25.422 18913-18913/com.free W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
10-07 11:58:25.423 18913-18913/com.khan.free W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

我不知道什麼和錯誤在哪裏。我該如何解決這個問題?

+0

'destination.transferFrom'返回什麼? 'source.size()'的價值是什麼? – pskink

+0

我怎麼檢查....我沒有調試,當調試到達這個聲明「源=新FileInputStream(currentDB).getChannel();」它會停止 – Dilazak

+0

其在這裏崩潰... – Dilazak

回答

0

做了一些調試後,我發現了正確的答案。我發佈了正確的答案。它可能有助於某人。

public void exportDB(){ 

    // it is the path to the existing database file in apps private storage 
    final File dbFile = context.getDatabasePath(DATABASE_NAME);//existing database file path 

    // this will give the devices internal storage as /storage/emulated/0 
    File device = Environment.getExternalStorageDirectory();//device storage 

    FileChannel source = null; 
    FileChannel destination = null; 

    try { 
     source = new FileInputStream(dbFile).getChannel(); 
     destination = new FileOutputStream(device).getChannel(); 
     destination.transferFrom(source, 0, source.size()); 
     source.close(); 
     destination.close(); 

     Toast.makeText(this,"Database is exported to device storage",Toast.LENGTH_LONG).show(); 
    } catch(IOException e) { 
     e.printStackTrace(); 
     Toast.makeText(this,"Failed to export database",Toast.LENGTH_LONG).show(); 
    } 
}