accessing android internal storage with cordova fi

2020-07-16 06:56发布

问题:

I'm trying to make an android app that, so far, is using the native voice recorder to record audio.

The path for that is the Sounds file in /storage/emulated/0/Sounds

Now the app is using the File Transfer cordova plugin. The root for that is /data/data/thisAppDirectory and requestFileSystem is using this as the path.

Is it possible to go up a directory with the file system to get to the sounds folder?

回答1:

Yes here we go! You have to utilize cordova file-transfer plugin, like so:

 window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs){
                    fs.root.getFile("'"+audioData[0].name+"'", {create: true, exclusive: false},
                      function(entry){
                        var fileTransfer = new FileTransfer();
                        fileTransfer.download(
                                "file:///storage/emulated/0/Sounds/" + audioData[0].name, // the filesystem uri you mentioned                  
                                "cdvfile://localhost/temporary/" + audioData[0].name,
                                function(entry) {
                                    // do what you want with the entry here
                                    console.log("download complete: " + entry.fullPath);
                                    window.requestFileSystem(LocalFileSystem.TEMPORARY, 1000000000, gotFS, fail);
                                },
                                function(error) {
                                    console.log("error source " + error.source);
                                    console.log("error target " + error.target);
                                    console.log("error code " + error.code + "Cheeeese");
                                },
                                false,
                                null
                        );
                    }, function(){
                        alert("file create error");
                    });
                }, null);