User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

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_code:file_picker

This is an old revision of the document!


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.

The code

filepicker.js
//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);
 
    //Add layout to app.    
    app.AddLayout( lay );
}//function Onstart()
 
 
function btn_OnTouch(){
    pick = new FilePicker(mycallback);
    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.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.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 dirlist=[];
    if( self.FolderPath != self.basePath ) dirlist = [".."];
    var fillist = [];
    var ths = lst.shift();
    while (undefined != ths) {
      var pth = folderPath + "/" + ths;
      if (app.IsFolder(pth))
         dirlist.push(ths);
      else
         fillist.push(ths);
      ths = lst.shift();
    }
    self.lstFolds.SetList(dirlist);
    self.lstFiles.SetList(fillist);
    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("/"));
  }
 
  this.parent.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.

sample_code/file_picker.1416667106.txt.gz · Last modified: 2014/11/22 22:38 (external edit)