app.ChooseFile sample?

372 views
Skip to first unread message

Bill DeWitt

unread,
Sep 1, 2018, 12:00:47 PM9/1/18
to DroidScript
Good morning folks!

When I run this clip (edited from the sample which also displays the same problem), I get a popup that says "null". Believing that the variable "fileName" was not filled, I  tried (and commented out) various ways to fill it. Probably my issue is that I don't understand callback. But shouldn't the sample have the file name in "fileName"?

Thanks for any help, I'll be reading about callback.


//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a choose file button.
btnFile = app.CreateButton( "Choose File", 0.5, 0.1 );
btnFile.SetMargins( 0, 0.05, 0, 0 );
btnFile.SetOnTouch( btnFile_OnTouch );
lay.AddChild( btnFile );
//Add layout to app.
app.AddLayout( lay );}

//Called when user touches our Choose File button.
function btnFile_OnTouch()
{
    app.ChooseFile( "Choose a file", "*/*", OnFileChoose );
//var fileName =  app.ChooseFile( "Choose a file", "*/*", OnFileChoose );
//var fileName =  app.ChooseFile( "Choose a file", "*/*", OnFileChoose(fileName) );
//OnFileChoose(fileName);
}

//Called when a file is chosen.
function OnFileChoose( fileName )
{
    app.ShowPopup( fileName );
}

Bill DeWitt

unread,
Sep 1, 2018, 12:53:24 PM9/1/18
to DroidScript
Now it seems that the problem might be with the file picker.  

One of the menu selections (below "Open From") is my stock File Manager, and when I use it to pick a file, it correctly returns a name.

I do have the current version of DroidScript (1.68), can it be that I need a plugin or something? I plan to go Pro on the 3rd of the month.


Steve Garman

unread,
Sep 1, 2018, 12:58:04 PM9/1/18
to DroidScript
The file picker is provided by the OS.

Your code exactly as posted works for me, whether I choose a file from the UI or from my file manager.

What version of Android are you using?

Bill DeWitt

unread,
Sep 1, 2018, 1:08:36 PM9/1/18
to DroidScript
Android 7.0

Can it be that the stock picker won't work from the SD card? Yeah, just now navigating to internal  storage it seems to work directly.

Steve Garman

unread,
Sep 1, 2018, 1:14:00 PM9/1/18
to DroidScript
Interesting. I've only ever tested app.Choosefile using Premium.

Can you let us know if that problem goes away if you join Premium and one of your apps requests extsdcard access please.

BareK

unread,
Sep 1, 2018, 1:14:45 PM9/1/18
to DroidScript
It works well on my device also.
I have no sdcard to test, but it could be the reason.

Callbacks are functions called when a specific task is over (or an event).
It can takes parameters that contains the result of the task or informations about the event.

Example:

app.SetOnError( OnError );

function OnError( msg, line, file )
{
    alert
( 'msg: ' + msg + '\n' + 'file: ' + file + '\n' + 'line: ' + line );
}

Bill DeWitt

unread,
Sep 1, 2018, 1:41:04 PM9/1/18
to DroidScript
Sure, I'll do that. I have to wait until after the 3rd for recurring charges, to make sure my disability check clears first.

Bill DeWitt

unread,
Sep 3, 2018, 11:16:50 AM9/3/18
to DroidScript
I just activated Premium, but the sample code still shows "null" when I choose a file. 
It never did ask to access the external drive. Is that something I should tell it to do in the program?

Bill DeWitt

unread,
Sep 3, 2018, 11:21:40 AM9/3/18
to DroidScript
OK, found "app.CheckPermission()" and set it up (I hope). It did check permission but still returned "null".

Jason Custer

unread,
Jan 27, 2020, 3:13:31 PM1/27/20
to DroidScript
Hey all! Haven't found a solution to this prob either, and I'd also like to allow the user to choose an app from their SDCard, not limmited to internal storage. As noted earlyer in this thread, ChooseFile works fine for the device's internal storage, but when choosing from the SDCard, available in the "show roots" option of the file picker dialog, I get null.

I also asked about this a few months ago, but nobody knew how to help me, so I gave it some time and google love. Google love got me here.
Here is my code, maybe I'm doing something wrong? My code is a mashup of the ChooseFile example, and the CheckPermissions example, with changes to combine the two. When I check permissions, I appear to have ExtSDcard and Storage permissions.
Any help is appreciated.

*******************************

function OnStart() {
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
txt = app.CreateButton( "Go" );
txt.SetOnTouch(go);
lay.AddChild(txt);
conf = app.CreateButton("Confirm");
conf.SetOnTouch(confirmPermissions);
lay.AddChild(conf);
app.AddLayout( lay );
} //onStart

go = function() {
    var check = app.CheckPermission("ExtSDcard,Storage");
if(check) {
alert("Some permissions are not ranted to this app. " + check);
return;
} //if
app.ChooseFile("Choose a file", "*/*", chosen);
} //go

chosen = function(path) {
app.Alert("file: " + path);
} //chosen

function confirmPermissions() {
    var check = app.CheckPermission("ExtSDcard,Storage");
if( check) {
app.GetPermission( check, PermissionResult );
} else {
alert( "everything ok!" );
} //if
} //confirmPermissions

function PermissionResult( ungranted ) {
alert( "ungranted: " + ungranted );
} //PermissionResult


*******************************

Thx!

Jason Custer

unread,
Feb 1, 2020, 12:11:37 PM2/1/20
to DroidScript
Hi all. As I wrote earlyer, app.ChooseFile doesn't work for me when users select from their SD Card. Not finding a solution, I wrote my own. There is an SPK on the droidscript store, https://ds.justplayer.de/uploads/30. I'd love to hear what people think. I am totally blind, so suggestions about appearence are also appreciated.
Enjoy!
Message has been deleted

Chennafo Hakim

unread,
Feb 5, 2020, 11:44:13 AM2/5/20
to DroidScript
If You Have ImageGrid Plugin Or Just app.CreateList(), So You Can Create Your Specific Code For ChooseFile .
For Example :
1. Get ListFolder Of Your Device .
2. And Create For Looping Code if(You Want Work Width Or Show Multiple List At The Same Time).

Note : "This Code Work With List";
var Path = "/sdcard/";
var Lst = app.ListFolder(Path, "",
null, "FullPath");

Files = app.CreateList(Lst, 1,1);
lay.AddChild(Files);

3. Add Files Touch Event , And After Any Touching So You Add This
Files.SetList(Path+item);//If item IsFolder .
Or item Is File So You Can Open It At Your Own App And Showing At Your Images Object Or Text Whenever .
Good Luck .


Reply all
Reply to author
Forward
Message has been deleted
0 new messages