User Tools

Site Tools


Privacy Policy


Version 2.50 is out since Jan 1st 2022

Frequently Asked Questions


Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax


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 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 );
    pick = new FilePicker(mycallback);
    // don't show hidden files
    //Add layout to app.    
    app.AddLayout( lay );
}//function Onstart()
function btn_OnTouch(){
}//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.lstFiles = app.CreateList("blank" , 0.35, 0.7 );
  this.lstFiles.parent = self;
  this.Show = function(){
    if(val == undefined) val = true;
    self.hideHiddenFiles = val;
  this.Hide = function(){
  this.SetFilter = function(filter){
      self.fileFilter = filter;
  this.GetFolder = function(){
    return self.FolderPath;
  this.SetFolder = function(folderPath){
    self.FolderPath = 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();
    if( self.FolderPath != self.basePath )
    while (undefined != ths) {
      if ((! self.hideHiddenFiles) || (ths.indexOf(".") != 0)){
        var pth = folderPath + "/" + ths;
        if (app.IsFolder(pth))
      ths = lst.shift();
}//function FilePicker()
function FilePicker_NewFolder(fil){
  var par = this.parent;
  var pth = par.GetFolder();
  if (fil != "..") {
      pth += "/" + fil
     if( pth == par.basePath || pth == "/" ){
     var tst = pth.split("/");
     tmp = tst.pop();
     pth = (tst.join("/"));
     if(pth=="") pth = "/";
}//function FilePicker_NewFolder()
function FilePicker_NewFile(fil){
  var par = this.parent;
  var pth = par.GetFolder();
  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);

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.

sample_code/file_picker.txt · Last modified: 2015/03/27 04:03 (external edit)