2

im trying to get the default music folders for all phonegap supported platforms. basically i can download and save file on the sdcard using the function below. but i want to add code to detect platform and give me default music folders for platform so as i can save mp3 file there.

    function downloadFile(remoteloc,new_name,userid,mid,errorbox,origname)
{
    window.requestFileSystem(
    LocalFileSystem.PERSISTENT, 0, 
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
        "dummy.html", {create: true, exclusive: false}, 
        function gotFileEntry(fileEntry) {
            var sPath = fileEntry.fullPath.replace("dummy.html","");
            var fileTransfer = new FileTransfer();
            fileEntry.remove();
            $('#'+errorbox).html("<div>"+origname+"</div><div class=\"progress progress-danger progress-striped\"><div id='id_p' class=\"bar\" style=\"width: 5%\"></div></div>");
            fileTransfer.onprogress = function(progressEvent) 
            { 
                if (progressEvent.lengthComputable) { 
                    $('#id_p').css('width',Math.ceil((progressEvent.loaded / progressEvent.total)*100)+"%"); 
                } else { 

                } 
            }
            fileTransfer.download(
            remoteloc,
            sPath + new_name,
            function(theFile) {
                $('#'+errorbox).html("<div>"+origname+"</div><div class=\"alert alert-info fade in\"><button class=\"close\" data-dismiss=\"alert\"><span class=\"awe-remove-circle\"></span></button>Download Complete. Added to Media Player</div>"
                +"<div><a href='"+theFile.toURI()+"' target='_blank' class=\"btn btn-success\">Play Song</a></div>"+"<br/>"+theFile.toURI());
                //update the database field to show file has been transfered
                if (!isOnline()) 
                    {
                    $('#error_box').html("<div class=\"alert alert-error fade in\"><button class=\"close\" data-dismiss=\"alert\"><span class=\"awe-remove-circle\"></span></button>Sorry but you seem to be offline.</div>");    
                    return;
                }
                var request={'controller':'music','action':'updatedownload','userid':userid,'mid':mid};
                queryAPI(request,function (d){
                    //check result and set local storage variables
                    if (d.success>0)
                        {
                    } else
                        {

                    }   
                    localStorage.removeItem('resume');
                    window.key=false;
                    //setTimeout(function () {$('#'+errorbox).html("<div>"+origname+"</div>");},3000);
                });             
            },
            function(error) {
                $('#'+errorbox).html("<div>"+origname+"</div><div class=\"alert alert-error fade in\"><button class=\"close\" data-dismiss=\"alert\"><span class=\"awe-remove-circle\"></span></button>Download Error! Error code:"+error.code+"</div>");
            }
            );
        }, 
        fail);
    },
    fail);
}
4

1 回答 1

1

Because of major platform differences, there isn't a "one time getDirectory call" solution to do this. The best way to handle this is to do a check for the platform you're currently running and write the file based on that.

var platform = device.platform;
switch(platform)
{
    case 'iPhone':
      //save to the app document folder
      break;
    case 'Android':
      //save to <external_storage_root>/Music
      break;
    case 'BlackBerry':
      //Save to /SDCard/BlackBerry
      break;
}

Be sure to check official documentation for the right file path's etc.

Also take note that while /var/root/Media/iTunes_Control/Music is sometimes mentioned as the default folder for music on iOS, it's managed by iTunes so it can be modified at any time and is only useful for temporary storage if it can be accessed at all. Using the documents folder is the prefered method.

于 2013-03-11T11:03:03.697 回答