Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 599
Dancer Avatar asked Dec 06 '22 17:12

Dancer


1 Answers

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.

like image 65
Simon MacDonald Avatar answered Dec 25 '22 17:12

Simon MacDonald