I needed a file chooser dialog box for an application I am working on. Android APIs do not have File Chooser dialog, and I had created my own native Android File Chooser dialog for my application Annotated Call Log. However I did not want to use that for my current application, because it needs to run on iOS too.
A quick search on the Web for PhoneGap based file chooser dialog did not give me what I was looking for, so I decided to create one using PhoneGap file APIs, JQuery and JQuery Mobile. You can find the source code here. The page won’t display much in the browser. Use ‘View Source’ of your browser to see the source code.
When I used this in my mobile application, it looks like this –
The current path is displayed at the top. The first entry in the file list is to go up one level.
I have made it configurable so that you can specify –
- If new file and or folder buttons should be displayed
- Set initial folder. This has to be DirectoryEntry
- If only folders to be displayed (for Directory Browser)
- Callback functions for file selection and directory selection. If you return false from these callback functions, then dialog is closed.
- Callback handler when OK is clicked. The argument to the callback function is currently selected DirectoryEntry in the dialog box.
All these parameter should be set in an Object called ‘file_Browser_params’
How to use this
You need to include necessary files for JQuery, JQuery Mobile and PhoneGap in your project or main file (that invokes File Chooser). Then create a button in the calling page to launch the browser –
<a href="fileBrowser.html" id="browseBtn" data-role="button" data-inline="true">Browse</a>
Then set parameters for the file chooser just before displaying it. You can do that by handling click event for ‘browseBtn’
$("#browseBtn").click(function(){ //check if last selected folder was set if (typeof lastFolderSelected == 'undefined') lastFolderSelected = null; //create file chooser dialog parameters file_Browser_params = { directory_browser : false, //this is file browser. Default is false new_file_btn : true, //show new file button. Default is true new_folder_btn : true, //shoe new folder button. Default is true initial_folder : lastFolderSelected, //initial folder when dialog is displayed //callback function when file is selected on_file_select : function(fileEntry) { return false; //close dialog when any file is selected (tapped) }, //callback function when folder is selected on_folder_select : function(dirEntry) { //don't do anything }, //callback function when OK button is clicked on_ok : function (dirEntry) { //save the last folder path lastFolderSelected = dirEntry; } };
All above parameters are optional, including file_browser_params object. This dialog box can be used as both File selection or Directory selection dialog box.
-Ram Kulkarni
Thx for making the effort to explain the terminlogy to the novices!
This doesn’t work as is. Could you please provide complete example files.
I have posted a complete example here. You will see files only when you run this code in PhoneGap application on mobile.
I have used the code exactly as you have and i still cannot browse on the device. Are there additional permissions etc that need to be set? I am using an iPhone 4S running iOS 6, Cordova 2.2.0rc1, jQuery mobile-1.2.0
I have tested this on Android ICS and it works. I had to set only one permission, which was android.permission.ACCESS_NETWORK_STATE
works well! thanks!
Hey Ram,
I was trying for the last twenty days to get your code work with eclipse, in fact it works on iOS, but not in android, although i set all the permissions I just can’t find where is the problem!
Can you please give me the whole android project so I can see where is the problem?
thanks a lot!
You can download the Android project from here
we are integrate file chooser app in my phonegap (cordova version 2.9.0) application it displays the file directory ,but file select option is not working it shows file_Browser_params.on_file_select is undefined.
please give me the reply as soon as possible.
I have already uploaded the complete sample code here. See index.html in assets foder of the project to know how to set parameters. Here is the relevant script block in that file –
Thanks a lot it works great! Maybe I’ve missed up the project configuration.
Again thank you.Ram
Hello
Is there any way to upload from selected File~
because what i’m gonna do is uploading file from mobileWebPage
if you knwo plz~ tell me
Thx~
Use FileTransfer from phonegap API.
Its not listing the folders and files in IOS , working only on android ,how to findout ??
can u please help me
Each application in iOS has access to its own private folder only, unlike in Android where applications have access to the entire file system. So you won’t see any file/folder in iOS using the sample code I postes, unless your application creates ones.
Great Tutorial, but can you please give a code sample of how i can select a file and upload it. My phonegap experience is not really good.
Your help will be appreciated.
Thanks
I don’t have a sample code ready for uploading file (I am assuming you want to upload to a server). Once you select a file using file chooser dialog, read the file content using PhoneGap file APIs and then send it to a server using XHR. JQuery has utility functions to post data to server.
Great Tutorial, I have just tested for android and its working fine.
Thanks a lot!
When I am trying to install on my Galaxy S Adavance 4.1.2 Jelly Bean. I am not able to view the directory structure. An alert is displaying with “[Object object]” message.
Can you identify the problem?
Thanks in advance.
I have uploaded new FileChooser project created with PhoneGap 3.3. I have tested it on Android 4.2.2 and it worked fine.
Nice tutorial. I searched last one week for this one. In this i got file path how to get the file type and its contents. Thank you.
When i read the type like fileEntry.type it shows undefined.
I don’t think FileEntry object has type field, see http://docs.phonegap.com/en/3.3.0/cordova_file_file.md.html#FileEntry
Yep . I found the solution. Thank you
Hi Ram…sorry to disturb. It works well but some times the file chooser shows empty(files are available in my device).Any idea.
Check JS console for any errors. Also you may want to set file_Browser_params.initial_folder = null before showing the file chooser dialog.
First Thanks for your quick reply Ram,
There is no error in Console. I need to create a variable and set like
var file_Browser_params.initial_folder = null ; Because it is already present in init function.
Again to sorry to disturb is that any limitation for picking files. When i pick more than 5MB application closed unexectedly.
It is not about which file you pick, I guess, but what you do after that. If you are trying to read a large file then application may be running out of memory
Thanks for your reply. I am able read all type of files. lot of persons having this issue now. Some of them posted in forums today.
Now working on this one. After pick file the file read as toDataURL. I think it creates issue if it exceeds some limit.
How to pick multiple files at the same time.
I haven’t implemented multiple file selection. But it is not too difficult. I guess you can modify the source code to support this.
yes done it…Thank you so much..
I tried your android project and it didn’t work. I’m looking to find a workaround for the webview bug that causes the input file type to not work at all. (It’s related to the openfilechooser api which they removed) The newer android project shown (3.3) seems empty. Could you help me with this problem?
I should mention this is on android 4.4.2
Download FileChooser project and unzip it. Open Android Developer Tools (Eclipse). Select menu File->Import. Then select Android->Existing Android Code …
Click Browse button next to Root Directory text box and select FileChooser/platforms/android and click Open button. You should see two projects (FileChooser and FileChooser-CordovaLib) listed and selected. Click Finish. Run the application on a device or emulator.
I have tested this on Android 4.2.2 and it works fine.
I’m using the adobe phonegap build service and it didn’t work there.
I am testing on a Google Nexus 5 32 GB with Android 4.4.2 kitkat.
It works when I run through adt and copy the apk. Any advice on how to include in build service?
Thanks Ram…wonderful sharing
Its working fine
Tanks.
Hi kulkarni
very nice artical but can you tell me how to read/write files from external SD card if its available.
Thanks
Nouman.
great ! thank you so much !
Thanks for such a good article and also working fine in android and ios, but in ios it will shows only NoCloud directory, how to change a root directory to applicationStorageDirectory path ?
I need a filechooser cordova plugin for iOS :S please somebody know about one (sorry for my english, I’m from Mexico)