2017-08-26 59 views
-2

我試圖重新鍵入是否有任何鍵入錯誤,但它仍然無法正常工作。 它甚至沒有提示我我有任何錯誤Android工作室將DB文件複製到內部存儲沒有打擾

我在想這是我的內部存儲路徑文件給出錯誤?

這是我想複製到的內部存儲文件的位置! Path file

這是我打電話給我的方法在功能的onclick:

else if (view == findViewById(R.id.btnBackup)){ 
     exportDatabase("stock.db"); 

    } 
} 

這是導出數據庫的方法:

public void exportDatabase(String databaseName) { 
    try { 
     File sd = getFilesDir(); 
     File data = Environment.getDataDirectory(); 

     if (sd.canWrite()) { 
      String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+""; 
      String backupDBPath = "//MyDb"; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 

      if (currentDB.exists()) { 
       FileChannel src = new FileInputStream(currentDB).getChannel(); 
       FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close(); 
      } 
      Toast.makeText(this, "Backup Done!", Toast.LENGTH_SHORT); 
     } 

    } catch (Exception e) { 
     Toast.makeText(this, "Fail!", Toast.LENGTH_SHORT); 
    } 
} 

回答

1

使用此方法的工作完美。

public void exportDB(Context mContext) { 
    try { 
     File sd = mContext.getExternalFilesDir("/database/"); 
     File data = Environment.getDataDirectory(); 

     if (sd.canWrite()) { 
      String currentDBPath = "//data//" + PACKAGE_NAME + "//databases//" + DATABASE_NAME; 
      String backupDBPath = DATABASE_NAME; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 

      FileChannel src = new FileInputStream(currentDB).getChannel(); 
      FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
      dst.transferFrom(src, 0, src.size()); 
      src.close(); 
      dst.close(); 
     } 
    } catch (Exception e) { 
    } 
} 
+0

thx查看信息 –

+0

@RickYuzuriha請接受我的答覆upvote –

相關問題