2015-11-14 29 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);