我编写了以下代码,以使用cordova在Android的cordova.file.externalRootDirectory目录下创建嵌套目录('ROOTapplication / pdf')。该代码在首次执行时仅创建“ ROOTapplication”。在第二次执行时,还会在ROOTapplication下创建pdf。我将一步创建('ROOTapplication / pdf')。遵循代码。
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", init, false);
var listaCartelle = new Array();
var rootApplication, esito;
function init()
{
esito = autorizzApp();
if(esito == "SUCCESS")
{
rootApplication = cordova.file.externalRootDirectory + 'ROOTapplication';
listaCartelle.push(cordova.file.externalRootDirectory, 'ROOTapplication');
listaCartelle.push(rootApplication, 'pdf');
localStorage.pdf = localStorage.rootApplication + '/pdf';
creaAlberaturaApp(creaCartellaSingola);
}
}
function creaAlberaturaApp(creaCartellaSingola)
{
var i=0;
while(i <= listaCartelle.length-2)
{
if (creaCartellaSingola && typeof creaCartellaSingola === 'function') {
esito = creaCartellaSingola(listaCartelle[i], listaCartelle[i+1]);
if(esito == "SUCCESS")
i+=2;
else
break;
}
}
}
// CREATE EXTERNAL DIR
function creaCartellaSingola(father_dir, children_dir)
{
// CREATE DIRECTORY "children_dir" UNDER "father_dir"
window.resolveLocalFileSystemURL(father_dir,
function fileEntryCallback(fileEntry) {
fileEntry.getDirectory(children_dir , { create: true, exclusive: false });
}
);
return "SUCCESS";
}
// AUTHORIZATION FOR WRITING ON SD
function autorizzApp()
{
cordova.plugins.diagnostic.requestExternalStorageAuthorization(function(status){
switch(status){
case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
alert("Permission not requested");
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
alert("Permission denied");
break;
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
debug("Permission granted");
break;
}
});
return "SUCCESS";
}
</script>