Reputation: 1158
I'm trying to build a mini File Explorer, I've got it working; but can only list the internal storage (local) - I can't seem to access the external SD Card.
Here's an example of the code I'm using:
function getFiles(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystem, function (evt){alert("ERRR... 1");});
}
function onRequestFileSystem(fileSystem) {
var directoryReader = fileSystem.root.createReader();
directoryReader.readEntries(onReadEntries, function (evt){alert("ERRR... 2");});
}
function onReadEntries(entries) {
for (var i = 0; i < entries.length; i++) {
console.log(entries[i].name+ " " + i);
}
}
1) How would I list the contents of my SD Card.
2) The S3 I'm using lists the SD card as extSdCard (img attached) - but I know other devices don't use this naming convention; the Xperia's SD card is /storage/sdcard1 - is there a consistent way of referencing external storage?
Upvotes: 0
Views: 2961
Reputation: 61
1) I figured out a solution to scan sdcard and gain access to the files by providing a directory like file://storage/sdcard1/ ( path to my SDcard ).
2) I don't have an answer.
I've submit an API and a sample project
https://github.com/xjxxjx1017/cordova-phonegap-android-sdcard-full-external-storage-access-library
Hope it could be helpful.
Example of using the API
new ExternalStorageSdcardAccess( fileHandler ).scanPath( "file:///storage/sdcard1/music" );
function fileHandler( fileEntry ) {
console.log( fileEntry.name + " | " + fileEntry.toURL() );
}
The API source code
var ExternalStorageSdcardAccess = function ( _fileHandler, _errorHandler ) {
var errorHandler = _errorHandler || _defultErrorHandler;
var fileHandler = _fileHandler || _defultFileHandler;
var root = "file:///";
return {
scanRoot:scanRoot,
scanPath:scanPath
};
function scanPath( path ) {
window.resolveLocalFileSystemURL(path, _gotFiles, errorHandler );
}
function scanRoot() {
scanPath( root );
}
function _gotFiles(entry) {
// ? Check whether the entry is a file or a directory
if (entry.isFile) {
// * Handle file
fileHandler( entry );
}
else {
// * Scan directory and add media
var dirReader = entry.createReader();
dirReader.readEntries( function(entryList) {
entryList.forEach( function ( entr ) {
_gotFiles( entr );
} );
}, errorHandler );
}
}
function _defultFileHandler(fileEntry){
console.log( "FileEntry: " + fileEntry.name + " | " + fileEntry.fullPath );
}
function _defultErrorHandler(error){
console.log( 'File System Error: ' + error.code );
}
};
Configurations
config.xml
delete preference: preference name="AndroidExtraFilesystems"
make sure testing environment can access it's own external storage at the time of testing. ( e.p. if you connect it with usb, make sure it is connected as a camera; Call the APIs after recieving the "deviceready" event )
Path example:
file:///
file:///somefile/
file:///somefile
file:///somefile/music/aaaaa.mp3
Upvotes: 1
Reputation: 1188
You should read this, it helped me a lot building an android file explorer.
It's a java code you should use with a javascriptInterface : Create a class implementing this code, and add this interface like this in the onCreate of your MainActivity :
myClass = new MyClass (this, appView, this);
appView.addJavascriptInterface(myClass , "myClass ");
Then, you'll be able to call it from javascript this way :
window.myClass .getAllStorageLocations();
Upvotes: 2