2014-03-05 60 views
0

我能夠將SD卡上的文件(Android 4.2)複製到另一個文件夾 - 但由於某些原因我無法訪問/ data/data/com中的文件.applicationname /目錄。 我的目標是將我的應用程序的sqlite數據庫備份到SD卡中以備以後檢索。 我的代碼:複製Android上的根文件夾中的文件與phonegap

function save_db() { 
    var dbfile = "file:///../app_database/file__0/0000000000000001.db"; // NOT working 
    var dbfile_test = "file:///../../../../sdcard/test.db"; // WORKS ! 
    var destDir = "file:///../../../../sdcard/backup"; 
    var newfile = "backup.db"; 

    window.resolveLocalFileSystemURI(dbfile, function(file) { 
     window.resolveLocalFileSystemURI(destDir, function(destination) { 
      alert("copy file "+newfile); 
      file.copyTo(destination,newfile); 
     },fail1) 
    },fail2); 

該功能以dbfile_test參考,但不能與DBFILE - 在這種情況下,我得到了「FAIL2」錯誤消息1 - 沒有找到。但該文件在那裏。 所以我認爲它必須與根目錄的訪問?應用程序是否必須擁有超級用戶權限?如果是的話,該怎麼做?

請指教 - 謝謝! Chris

回答

0

在我閱讀正確之前已經回答。這當然是無用的,因爲它是基於Java的解決方案。

我用的是這樣的:

File dbFile = new File("/data/data/package.application/databases/databasename"); 
File destFile = new File("sdcard/backup/backup.db"); 
FileChannel src = new FileInputStream(dbFile).getChannel(); 
FileChannel dest = new FileOutputStream(destFile).getChannel(); 
if (dbFile.exists()) { 
    dest.transferFrom(src, 0, src.size()); 
} 
src.close(); 
dest.close(); 

try catch塊必須被使用。

+0

謝謝無論如何 - 不幸的是,我還沒有弄清楚如何從JavaScript內部調用Java函數,如果這是可能的話;-) – Chris

相關問題