cssnewbie
cssnewbie

Reputation: 5

cordova create a folder inside sdcard - internal or external

function creatingFolder(fileSystem) {
           var entry = fileSystem.root;
            entry.getDirectory("productpictures", {create: true, exclusive: false}, win, error);
            window.newfolder = fileSystem.root.toURL()+"/productpictures";

        }

       function win(dir) {
          alert("Created dir with name: "+dir.name);
          alert("Created dir at: "+dir.toURL());
          alert("Created dir NativePath: " + dir.nativeURL);
          alert('done '+window.newfolder);
        }

       function error(error){
              alert('hmm: '+error.code+' message: '+error.message);
       }

Ok, so [productpictures] is a folder my app will create and the app users can download files into this [productpictures] folder. My question is how do i allow my app users to access this folder [productpictures] after they close the app. Now, when i create this folder on a real Android device, the path is: file:///data/data/com.packagename/files/files/productpictures.

So is there any way we can create this folder somewhere else where Android device users can easily access even after they close the app. I want to create this folder [productpictures] in place like sdcard/productpictures or into Android Photo gallery or Download folder of Android device.

Another code which i tried, but didn't work;

function creatingFolder(fileSystem) {
           var entry = fileSystem.root;
            entry.getDirectory(cordova.file.externalRootDirectory+"/productpictures", {create: true, exclusive: false}, win, error);
            window.newfolder = cordova.file.externalRootDirectory+"/productpictures";

        }

So, haven't found any resource online to accomplish this. I want this feature because the users should be able to email or share the files inside [productpictures] folder and having this folder inside a location like file://data/data/com.package/files/files/productpictures is too complex.

Thanks for help in advance.

Upvotes: 0

Views: 1760

Answers (1)

Gandhi
Gandhi

Reputation: 11935

This sample code lets you create folder in the external root directory in Android and documents folder 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: 2

Related Questions