使用cordova读取内部存储文件android(resolveLocalFileSystemURL)

Car*_*ado 1 android cordova

我搜索并尝试了很多东西,只是为了使用 resolveLocalFileSystemURL 检索任何路径的文件的内容

小路

该文件位于:storage/emulated/0/miniclipld.txt

但我无法访问它,尝试过:

window.resolveLocalFileSystemURL('file:///storage/emulated/0/miniclipld.txt', function(fileEntry){
            fileEntry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    console.log(this.result); // text
                };
                reader.readAsText(file);
            });
        }, function(err){
            console.error(err);
            //error
        });
Run Code Online (Sandbox Code Playgroud)

太努力了

window.resolveLocalFileSystemURL('/storage/emulated/0/miniclipld.txt', function(fileEntry){
            fileEntry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    console.log(this.result); // text
                };
                reader.readAsText(file);
            });
        }, function(err){
            console.error(err);
            //error
        });
Run Code Online (Sandbox Code Playgroud)

无论如何,返回的代码是 1 或 5,是文件路径不正确还是什么?任何帮助表示赞赏!

Car*_*ado 5

问题只是在 filaname 中的一个错字...以下结构是正确的。如果有人想知道如何从 android 中的任何路径读取文件,请使用以下代码

        window.resolveLocalFileSystemURL('file:///storage/emulated/0/miniclipId.txt', gotFile, fail);

        function fail(e) {
            console.log("FileSystem Error");
            console.dir(e);
        }

        function gotFile(fileEntry) {
            fileEntry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    var content = this.result;
                    console.log(content);
                };
                reader.readAsText(file);
            });
        }
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 - 在尝试修复这个 Android 错误时为我节省了很多时间 - 我在文件 URI 前面加上了“file://” - window.resolveLocalFileSystemURL("file://"+fileURI, success , fail) 在 Android 中它修复了问题给我。 (2认同)