简体   繁体   English

Cordova:无法使用Cordova在Android上复制文件

[英]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. 在尝试设置以下权限时,我不知何故得到“错误代码5”。 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). 我想要做的是将现有文件从android中的资产复制到Android设备上的可访问位置,以便能够在其他应用程序(如邮件)之间共享它。

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插件吗?,您可以使用blob来读取文件内容,而不是使用cordovaFile插件在android sdcard上写一个新内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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