简体   繁体   中英

Cordova: Not able to copy file on Android using Cordova

Somehow I keep getting an "error code 5" when trying to set the following right. What I want to do, is copy an existing file from the assets in android to an accessible spot on the android device to be able to share it across other apps (like mail).

Here is my code example:

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    var storagefolder = cordova.file.dataDirectory;
    var storagefolderpointer;
    console.log("storage folder: " + storagefolder);

    // Check for support.
    if (window.requestFileSystem) {
        console.log("filesystem beschikbaar");
        var getFSfail = function () {
            console.log('Could not open filesystem');
        };
        var getFSsuccess = function(fs) {

            var getDIRsuccess = function (dir) {
                    console.debug('Got dirhandle');
                    cachedir = dir;
                    fileurl  = fs.root.fullPath + '/' + storagefolder;
                    storagefolderpointer = dir;
            };
            var getDIRfail = function () {
                console.log('Could not open directory');
            };

            console.debug('Got fshandle');
            FS = fs;
            FS.root.getDirectory(storagefolder, {create:true,exclusive:false}, getDIRsuccess, getDIRfail);
        };
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getFSsuccess, getFSfail);

        setTimeout(function() {

            console.log("directory beschikbaar");
            var suc = function(entry){
                var goe = function(){
                    console.log("copy success");
                };
                var fou = function(){
                    console.log("copy NOT NOT success");
                };
                entry.copyTo(storagefolder, "vcard.vcf", goe, fou);
            };
            var fai = function(e){
                console.log("fail getFile: " + e.code);
            };
            window.resolveLocalFileSystemURL(storagefolderpointer + "www/visitekaart/vcard.vcf", suc, fai);

        }, 1000);

    } else {
        console.log("filesystem NOT NOT NOT available");
    }

have you use cordovaFile plugin instead?,you can use blob to read the content of your files than write a new one on android sdcard using cordovaFile plugin

$cordovaFile.writeFile('appdata/file.txt', blob, 0).then(function(fileEntry) {
    //success
}, function(err) {
    //err
}

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