简体   繁体   中英

Copy file in root folder on android to sdcard with phonegap

I'm able to copy a file on the sdcard (Android 4.2) to another folder - BUT for some reason I cannot access a file in the /data/data/com.applicationname/ directory. My aim is to backup the sqlite database of my application to sdcard for later retrieval. My code:

 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);

The function works with dbfile_test reference but NOT with dbfile - in that case I get the "fail2" error message 1 - not found. But the file is there. So I assume it has to do with the access to the root directory ? Has the application have to have superuser rights ? If yes, how to do that ?

Please advice - thank you ! Chris

Answered before I read correctly. This is of course useless as it is Java based solution.

I use something like this:

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 block must be used as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM