====== Sample File Picker ======
This is a simple file choosing dialog which is offered for use on DroidScript v1.12
When the next version of DroidScript is released, I will tidy it up a bit and add a filter for file extensions.
Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works.
See also [[sample_code:folder_picker|Sample Folder Picker]]
===== The code =====
//Called when application is started
function OnStart(){
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "Vertical,FillXY" );
//Create a text label and add it to layout.
btn = app.CreateButton( "FilePicker demo" );
lay.AddChild( btn );
btn.SetOnTouch(btn_OnTouch);
pick = new FilePicker(mycallback);
// don't show hidden files
pick.SetHideFiles(true);
//Add layout to app.
app.AddLayout( lay );
}//function Onstart()
function btn_OnTouch(){
pick.SetFolder("/sdcard");
pick.Show();
}//function btn_OnTouch()
function mycallback(fullpath){
app.Alert("user chose " + fullpath)
}//function mycallback()
function FilePicker(Callback,basePath){
var self = this;
this.basePath = basePath || "/sdcard";
this.callback = Callback || function(){};
this.FolderPath = this.basePath;
this.hideHiddenFiles = false;
this.dlg = app.CreateDialog(this.basePath);
this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" );
this.lstFolds = app.CreateList("blank" , 0.35, 0.7 );
this.lstFolds.parent = self;
this.lstFolds.SetOnTouch(FilePicker_NewFolder);
this.lay.AddChild(this.lstFolds);
this.lstFiles = app.CreateList("blank" , 0.35, 0.7 );
this.lstFiles.parent = self;
this.lstFiles.SetOnTouch(FilePicker_NewFile);
this.lay.AddChild(this.lstFiles);
this.dlg.AddLayout(this.lay);
this.Show = function(){
self.dlg.Show();
}
this.SetHideFiles=function(val){
if(val == undefined) val = true;
self.hideHiddenFiles = val;
}
this.Hide = function(){
self.dlg.Hide();
}
this.SetFilter = function(filter){
self.fileFilter = filter;
}
this.GetFolder = function(){
return self.FolderPath;
}
this.SetFolder = function(folderPath){
self.FolderPath = folderPath;
self.dlg.SetTitle(folderPath);
app.ShowProgress( "Loading..." );
var lst = app.ListFolder(folderPath);
lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1});
var ths = lst.shift();
self.lstFolds.SetList("");
if( self.FolderPath != self.basePath )
self.lstFolds.AddItem("..",null,"folder");
self.lstFiles.SetList("");
while (undefined != ths) {
if ((! self.hideHiddenFiles) || (ths.indexOf(".") != 0)){
var pth = folderPath + "/" + ths;
if (app.IsFolder(pth))
self.lstFolds.AddItem(ths,null,"folder")
else
self.lstFiles.AddItem(ths)
}
ths = lst.shift();
}
app.HideProgress();
}
}//function FilePicker()
function FilePicker_NewFolder(fil){
var par = this.parent;
var pth = par.GetFolder();
if (fil != "..") {
pth += "/" + fil
}
else{
if( pth == par.basePath || pth == "/" ){
par.Hide()
return;
}
var tst = pth.split("/");
tmp = tst.pop();
pth = (tst.join("/"));
if(pth=="") pth = "/";
}
par.SetFolder(pth);
}//function FilePicker_NewFolder()
function FilePicker_NewFile(fil){
var par = this.parent;
var pth = par.GetFolder();
par.Hide();
par.callback(pth += "/" + fil);
}//function FilePicker_NewFile()
===== How to use it =====
You need three function from the code above
function FilePicker
function FilePicker_NewFolder
function FilePicker_NewFile
In your main code, you need something like
pick = new FilePicker(myCallBack);
pick.SetFolder("/sdcard");
pick.Show();
where myCallBack is a function which is called when a file is chosen. It is defined like
function myCallBack(fullpath){
app.ShowPopup("user chose " + fullpath)
}
and it passes you the full path of the file that is chosen.