2011-06-22 61 views
0

是否可以將.db文件(如mmssms.dbuser_dict.db)複製到SDCard?如何備份.db文件到SDCard?

如果可能,那麼我所需要做的就是讀取db文件並將其寫入SDCard。

+0

http://stackoverflow.com/questions/2170031/backup-and-restore-sqllite-database-to-sdcard – kgiannakakis

回答

1

您可以使用此示例開始:

try { 
    File sd = Environment.getExternalStorageDirectory(); 
    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); 

     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(); 
     } 
    } 
} catch (Exception e) { 
    // exception 
} 
+0

感謝的可能的複製對於響應...我在運行時遇到'java.io.FileNotFoundException:/data//data/com.android.providers.telephony/databases/mmssms.db(Permission denied)'錯誤。我需要爲此設置什麼權限... – Dinash

+0

@Dinash WRITE_EXTERNAL_STORAGE是需要的,例如...但是mmssms.db不適用於任何Android設備上的開發人員。 – evilone

+0

雅我已經添加,但我認爲權限被拒絕時,從該路徑讀取... – Dinash

1
// Local database 
    InputStream input = new FileInputStream(from); 

    // create directory for backup 
    File dir = new File(DB_BACKUP_PATH); 
    dir.mkdir(); 

    // Path to the external backup 
    OutputStream output = new FileOutputStream(to); 

    // transfer bytes from the Input File to the Output File 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = input.read(buffer))>0) { 
     output.write(buffer, 0, length); 
    } 

    output.flush(); 
    output.close(); 
    input.close(); 
+0

http://www.screaming-penguin.com/node/7749 – Android