fobus
fobus

Reputation: 2058

Phonegap On Android 6 - How to get external storage path

I'm trying to get external storage path in Phonegap. I'm using this code but it never handles the correct SDCard path.

(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));
    }
})();

also

<preference name="AndroidPersistentFileLocation" value="Internal" />

and

<preference name="AndroidPersistentFileLocation" value="Emulated" />

doesn't help.

Are there a way to get the correct SDCard path on Android 6 with phonegap?

Upvotes: 0

Views: 1312

Answers (2)

Gandhi
Gandhi

Reputation: 11935

This is how i made it work in android as well as in iOS,

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
        }

Upvotes: 1

Anu
Anu

Reputation: 109

Try out this one.

document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}

function onFileSystemSuccess(fileSystem) {
    fileSystem.root.getDirectory("App_files", {create: false, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
}

function fail(evt) {
    console.log(evt.target.error.code);
}

var onGetDirectoryWin = function(parent) {

}
var onGetDirectoryFail = function() {
    console.log("error getting dir")
}

Upvotes: 0

Related Questions