Last active
January 31, 2019 00:00
-
-
Save casevictor/288fe47abc4fac6203c5 to your computer and use it in GitHub Desktop.
Titanium - How to list all your files/folders from your SD Card
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mkdfs = require("dk.mikkendigital.mkdfs"); // <- this module allows you to acess the SD Card root, without the AppDirectory | |
exports.showFileSystem = function(){ | |
//Window that will be pop-up with a tableview | |
var win = Ti.UI.createWindow({ | |
height:'100%', | |
width:'100%', | |
}); | |
//I forgot, but first test if externalStorage exists ok? | |
var dir = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory); | |
var directoryStr = dir.getParent().getDirectoryListing(); | |
var directoryArr = directoryStr.toString().split(','); | |
var dirData = []; | |
for (var i=0; i<directoryArr.length; i++) { | |
Titanium.API.info(directoryArr[i]); | |
var row = Ti.UI.createTableViewRow({ | |
title: directoryArr[i], | |
value: 'file://'+mkdfs.externalStorageDirectory + Titanium.Filesystem.separator, | |
}); | |
dirData.push(row); | |
}; | |
var tableDirs = Ti.UI.createTableView({ | |
height:'100%', | |
width:'100%', | |
}); | |
tableDirs.addEventListener('click', function(e){ | |
var newRows = []; | |
var secDir = Titanium.Filesystem.getFile(e.rowData.value + e.rowData.title); | |
//Test if secDir is a File or really a Directory | |
if(secDir.isFile()){ | |
Ti.API.info( secDir.nativePath + ' ' + secDir.name); | |
//Do anything with the file, like call a function.. | |
return; | |
} | |
var secDirStr = secDir.getDirectoryListing(); | |
for (i=0, max=secDirStr.length; i<max; i++){ | |
var row = Ti.UI.createTableViewRow({ | |
title: secDirStr[i].toString(), | |
value: e.rowData.value + e.rowData.title + Titanium.Filesystem.separator, | |
}); | |
newRows.push(row); | |
} | |
tableDirs.setData([]); | |
tableDirs.setData(newRows); | |
}); | |
tableDirs.setData(dirData); | |
win.add(tableDirs); | |
win.open(); | |
}; | |
//Credits | |
//I started with this code : | |
//http://stackoverflow.com/questions/11162324/appcelerator-android-unable-to-list-second-level-directories-with-filesystem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment