首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android 6上的Phonegap -如何获取外部存储路径

Android 6上的Phonegap -如何获取外部存储路径
EN

Stack Overflow用户
提问于 2016-03-24 01:46:23
回答 2查看 1.3K关注 0票数 0

我正在尝试获取Phonegap中的外部存储路径。我正在使用这段代码,但它从未处理过正确的SDCard路径。

代码语言:javascript
运行
复制
(function(){
    window.appRootDirName = ".myapp";
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        alert(cordova.file.externalRootDirectory);
        console.log("device is ready");
        window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function fail() {
        console.log("failed to get filesystem");
    }

    function gotFS(fileSystem) {
        console.log("filesystem got");
        fileSystem.root.getDirectory(window.appRootDirName, {
            create : true,
            exclusive : false
        }, dirReady, fail);
    }

    function dirReady(entry) {
        window.appRootDir = entry;
        alert(JSON.stringify(window.appRootDir));
    }
})();

也是

代码语言:javascript
运行
复制
<preference name="AndroidPersistentFileLocation" value="Internal" />

代码语言:javascript
运行
复制
<preference name="AndroidPersistentFileLocation" value="Emulated" />

帮不上忙。

有没有办法在安装了phonegap的Android6上获得正确的SDCard路径?

EN

Stack Overflow用户

发布于 2016-03-24 12:36:45

这就是我让它在安卓和iOS中工作的方式,

代码语言:javascript
运行
复制
function writeFile() {
            if (sessionStorage.platform.toLowerCase() == "android") {
                window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
            } else {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
            }

        }    

        function onError(e) {
            alert("onError");
        };

        function onFileSystemSuccess(fileSystem) {
            var entry = "";
            if (sessionStorage.platform.toLowerCase() == "android") {
                entry = fileSystem;
            } else {
                entry = fileSystem.root;
            }
            entry.getDirectory("Folder_Name", {
                create: true,
                exclusive: false
            }, onGetDirectorySuccess, onGetDirectoryFail);
        };

        function onGetDirectorySuccess(dir) {
            dir.getFile(filename, {
                create: true,
                exclusive: false
            }, gotFileEntry, errorHandler);
        };

        function gotFileEntry(fileEntry) {
            // logic to write file in respective directory
        };

        function errorHandler(e) {
            // handle error
        }
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36185217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档