2015-11-14 23 views
0

我試圖從外部驅動器(USB)導入數據庫文件,以恢復備份數據。該文件存在和路徑是正確的,但數據庫沒有進口的,這是我的代碼:Android從外部存儲器導入數據庫

File usb = new File("/storage/UsbDriveA/BackupHM/database.db"); 
File currentDB = getDatabasePath("database.db"); 

FileChannel source = null; 
FileChannel destination = null; 
String backupDBPath = "/storage/UsbDriveA/BackupHM/database.db"; 
File backupDB = new File(usb, backupDBPath); 
         try { 
          source = new FileInputStream(backupDB).getChannel(); 
          destination = new FileOutputStream(currentDB).getChannel(); 
          destination.transferFrom(source, 0, source.size()); 
          source.close(); 
          destination.close(); 
          Toast toast1 = Toast.makeText(getApplicationContext(), ("Restore ok"), Toast.LENGTH_SHORT); 
          toast1.show(); 
          finish(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
          Toast toast = Toast.makeText(getApplicationContext(), ("Error!"), Toast.LENGTH_LONG); 
          toast.show(); 
         } 
        } 
+0

你看到發生了什麼故障?發佈的代碼是否拋出IOException?我複製了您的代碼併成功傳輸了一個文件。 –

回答

0

編碼錯誤。聲明塊改成這樣:

 //File usb = new File("/storage/UsbDriveA/BackupHM/database.db"); 
     File currentDB = getDatabasePath("database.db"); 

     FileChannel source = null; 
     FileChannel destination = null; 
     String backupDBPath = "/storage/UsbDriveA/BackupHM/database.db"; 
     File backupDB = new File(backupDBPath);