Dancer
Dancer

Reputation: 17651

FIle API - Phonegap?

Can anybody explain how to list a folder of files on a page using the Phonegap File API for Android?

I would like to list all .mp3 files if possible, but have read through all the phonegap docs (http://docs.phonegap.com/en/1.0.0/phonegap_file_file.md.html) and cant figure it out at all!

Upvotes: 4

Views: 7284

Answers (4)

Manmohan Soni
Manmohan Soni

Reputation: 6621

Hi I use this code and it works for me .. can try it

$('#recordingList').on('pagecreate', function(event) {
    generateRecordingList();
});

function generateRecordingList(){
    //request for the file system 
    window.resolveLocalFileSystemURI("file:///sdcard/rem", onFileSystemSuccess, onError);
}

function onFileSystemSuccess(fileSystem){
    alert("FileSystem success : " + fileSystem.name);

}

function onError(error){
    alert(error.target.error.code);
}

Upvotes: 0

yogibimbi
yogibimbi

Reputation: 607

You could also save one step in Simon's code if you just wanted a proof of concept, since fileSystem.root is already a dirEntry (and it saves you the potentially buggy step of having to use a directory name such as "Music" which might or might not be there):

function onFileSystemSuccess(fileSystem) {
    var directoryReader = fileSystem.root.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(readerSuccess,fail);
}

function readerSuccess(entries) {
    var i;.......

which works for me.

However, what I get are the contents of the root directory of the app, apparently, my app being called be.qan.blaActivity, so, if I ask it for fileSystem.root.name it tells me "be.qan" and the fileSystem.root.path is undefined. Thus, my question here would be: how can I break out of this sandbox, if that is indeed what I am sitting in, and can I access the sd_card directories or other parts of the file system (since iOS still does not even have external storage)?

What I am getting by way of file names in readerSuccess are names such as "databases", "app_database", "cache" and "lib" which do not even show up when I do a search with Astro FileManager, so I strongly suppose they are part of a virtual file system that Cordova is creating for the app or so. Probably there is still a lot I am missing and folks more knowledgeable than I can set me straight, but that's what I am guessing so far.

Upvotes: 2

Will
Will

Reputation: 21

Paul, did you add a fail() function? That will be required in Simon's example code. The api docs example has:

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

Upvotes: 2

Simon MacDonald
Simon MacDonald

Reputation: 23273

it is a bit of a pain in the @$$ but doable. Start with this code:

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

// PhoneGap is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}

function onFileSystemSuccess(fileSystem) {
    fileSystem.root.getDirectory("Music", {create: false, exclusive: false}, getDirSuccess, fail);
}

function getDirSuccess(dirEntry) {
    // Get a directory reader
    var directoryReader = dirEntry.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(readerSuccess,fail);
}

function readerSuccess(entries) {
    var i;
    for (i=0; i<entries.length; i++) {
        // Assuming everything in the Music directory is an mp3, go nuts
        // otherwise check entries[i].name to see if it ends with .mp3
    }
}

I'm writing all this example code outside of an IDE so there may be bugs but that should get you going.

Upvotes: 11

Related Questions