javascript - Accessing user external sd card with cordova -
i'm working on little android application based on cordova , cordova-file plugin. i'm trying access user's files list of them (musics, pictures, media gallery).
i managed access content located on internal sd card, can't figure out clean way access removable/additionnal sd card.
the different paths using cordova.file :
{ applicationdirectory: "file:///android_asset/" applicationstoragedirectory: "file:///data/data/com.lbt.www/" cachedirectory: "file:///data/data/com.lbt.www/cache/" datadirectory: "file:///data/data/com.lbt.www/files/" documentsdirectory: null externalapplicationstoragedirectory: "file:///storage/emulated/0/android/data/com.lbt.www/" externalcachedirectory: "file:///storage/emulated/0/android/data/com.lbt.www/cache/" externaldatadirectory: "file:///storage/emulated/0/android/data/com.lbt.www/files/" externalrootdirectory: "file:///storage/emulated/0/" shareddirectory: null synceddatadirectory: null tempdirectory: null }
but none of allows access additionnal sd card content.
by looking @ folder tree of phone, found out additionnal sd card's content can found in 'file:///storage/extsdcard' or 'file:///mnt/extsdcard'.
knowing managed access it, don't think code adaptative. because i'm using static variable root path 'file:///'.
var app = { // vars folders: 0, // number of folder found during scan scannedfolders: 0, // number of folder scanned directories: [], // directories scan // launch app when device ready init: function(){ document.addeventlistener('deviceready', this.ready, false); }, // launch directories listing ready: function(){ var _this = app; _this.listdirectories(); }, // list directories in we'll files want listdirectories: function(){ var directory; // loop through cordova.file directories for(directory in cordova.file){ var current = cordova.file[directory]; if(current == null || typeof current == 'undefined') continue; current = this.stripdirectory(current); if(this.directories.indexof(current) != -1) continue; this.directories.push(current); } this.initscan(); }, // launch scan each principal directories initscan: function(){ var _this = this; var = 0; var nbdirectories = this.directories.length; for(i; < nbdirectories; i++){ var directory = this.directories[i]; window.resolvelocalfilesystemurl(directory, function(dir, callback){ _this.folders++; _this.scandirectory(dir); }); } }, // scan given directory files or other directories scandirectory: function(directory){ var _this = this; // initiate reader var reader = directory.createreader(); // list entries in directory reader.readentries(function(entries){ var = 0; var nbentries = entries.length; // loop through each entries in directory for(i; < nbentries; i++){ var entry = entries[i]; // if entry directory, scan directory if(entry.isdirectory){ _this.folders++; _this.scandirectory(entry); } // if entry file, check type else{ // stuffs if(entry.fullpath.indexof('\.mp3') != -1) console.log(entry.fullpath); } } // directory scanned ! _this.scannedfolders++; // check if folders have been scanned if(_this.scannedfolders == _this.folders){ console.log('scan finished'); } }); }, // strip directory 1 folder after root stripdirectory: function(directory){ directory = directory.split('file:///')[1].split('/')[0]; return 'file:///' + directory + '/'; }, }; // initialize app app.init();
does has better , more modulable approach achieve ?
Comments
Post a Comment