User Tools

Site Tools


sample_code:file_picker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:file_picker [2015/02/19 16:06]
stevegarman [The code]
sample_code:file_picker [2015/03/27 04:03]
Line 1: Line 1:
-====== 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 ===== 
-<code javascript 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); 
-    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("/")); 
-  } 
-   
-  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() 
- 
-</code> 
- 
-===== How to use it ===== 
-You need three function from the code above 
-   
-<code>function FilePicker</code> 
- 
-<code>function FilePicker_NewFolder</code> 
-  
-<code>function FilePicker_NewFile</code> 
-     
-     
-In your main code, you need something like 
- 
-<code>    pick = new FilePicker(myCallBack); 
-    pick.SetFolder("/sdcard"); 
-    pick.Show();</code>     
- 
-where myCallBack is a function which is called when a file is chosen. It is defined like 
- 
-<code>function myCallBack(fullpath){ 
-    app.ShowPopup("user chose " + fullpath) 
-}</code> 
- 
-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)