The Problem
I have a program that I wanted the user to do both selecting files and selecting directories; the libraries that I found offered different UI for each action ( selecting file / folders) which is not visually suitable for the user.
The Solution
I had to write my own library to select those files and folders. This is its Git repo "MyFileChooser" where you can see the code and download the jar file.
How to use
How to set up the library in your project download the jar file
Import the jar file into your project
- Right click on your project.
- Select properties
- Select "Java Build Path" then "Libraries" tab and then choose "Add External JARs"
- Choose the jar file that you downloaded.
- Add the following lines into you manifest file
<activity android:name="com.john_aziz57.myfilechooser.MainActivity" android:exported="true" > </activity>
- Since you will probably will read or write files don't forget to add the appropriate permissions
- The place where you want to start the activity add the following code to select folders
Intent i = new Intent(YourCurrentActivity, com.john_aziz57.myfilechooser.MainActivity.class); i.putExtra(MainPresenter.TAG, MainPresenter.SELECT_FOLDER); startActivityForResult(i, FOLDER_SELECT_CODE);
to select files
Intent i = new Intent(YourCurrentActivity, com.john_aziz57.myfilechooser.MainActivity.class); i.putExtra(MainPresenter.TAG, MainPresenter.SELECT_FILE); startActivityForResult(i, FILE_SELECT_CODE);
in "onActivityResult" add the following code
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
// Get the path
String path = uri.getPath();
.......
Screen Shots
here are screen shots inside my Absence program. first one for selecting files and the second one is for selecting folders
Final Notes
I don't expect that this project will meet up with all your needs, feel free to download the code and adjust it as much as you want or comment below for suggested enhancements
No comments:
Post a Comment