Android Library to select files/directories from Device Storage
Follow me on instagram to stay up-to-date https://instagram.com/a.masram444
Library available at JitPack.io
Latest version of this library is migrated to androidx
- Easy to Implement.
- No permissions required.
- Files, Directory Selection.
- Single or Multiple File selection.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.TutorialsAndroid:FilePicker:v9.0.0'
}
If you are targeting Android 10 or higher. Set this to your manifest
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application
android:requestLegacyExternalStorage="true" ........ >
......
</application>
</manifest>
-
Start by creating an instance of
DialogProperties
.DialogProperties properties = new DialogProperties();
Now 'DialogProperties' has certain parameters.
-
Assign values to each Dialog Property using
DialogConfig
class.properties.selection_mode = DialogConfigs.SINGLE_MODE; properties.selection_type = DialogConfigs.FILE_SELECT; properties.root = new File(DialogConfigs.DEFAULT_DIR); properties.error_dir = new File(DialogConfigs.DEFAULT_DIR); properties.offset = new File(DialogConfigs.DEFAULT_DIR); properties.extensions = null; properties.show_hidden_files = false;
-
Next create an instance of
FilePickerDialog
, and passContext
andDialogProperties
references as parameters. Optional: You can change the title of dialog. Default is current directory name. Set the positive button string. Default is Select. Set the negative button string. Defalut is Cancel.FilePickerDialog dialog = new FilePickerDialog(MainActivity.this, MainActivity.this,properties); dialog.setTitle("Select a File");
-
Next, Attach
DialogSelectionListener
toFilePickerDialog
as below,dialog.setDialogSelectionListener(new DialogSelectionListener() { @Override public void onSelectedFilePaths(String[] files) { //files is the array of the paths of files selected by the Application User. } });
An array of paths is returned whenever user press the
select
button`. -
Use
dialog.show()
method to show dialog.
Marshmallow and above requests for the permission on runtime. You should override onRequestPermissionsResult
in Activity/AppCompatActivity class and show the dialog only if permissions have been granted.
//Add this method to show Dialog when the required permission has been granted to the app.
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String permissions[],@NonNull int[] grantResults) {
switch (requestCode) {
case FilePickerDialog.EXTERNAL_READ_PERMISSION_GRANT: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(dialog!=null)
{ //Show dialog if the read permission has been granted.
dialog.show();
}
}
else {
//Permission has not been granted. Notify the user.
Toast.makeText(MainActivity.this,"Permission is Required for getting list of files",Toast.LENGTH_SHORT).show();
}
}
}
}
If your app targets Android 13 or higher and needs to access media files that other apps have created, you must request one or more of the following granular media permissions instead of the READ_EXTERNAL_STORAGE permission
:
As of Android 13 and above you can only browse and select Images,Videos and Audio files only. This library is still in development and I'm looking for contributors to make this library more better
Type of media | Permission to request
Images and photos | READ_MEDIA_IMAGES
Videos | READ_MEDIA_VIDEO
Audio files | READ_MEDIA_AUDIO
Before you access another app's media files, verify that the user has granted the appropriate granular media permissions to your app.
If you request both the READ_MEDIA_IMAGES
permission and the READ_MEDIA_VIDEO
permission at the same time, only one system permission dialog appears.
If your app was previously granted the READ_EXTERNAL_STORAGE
permission, then any requested READ_MEDIA_*
permissions are granted automatically when upgrading.
That's It. You are good to proceed further.
defaultValue
,error_dir
,root_dir
,offset_dir
must have valid directory/file paths.defaultValue
paths should end with ':'.defaultValue
can have multiple paths, there should be a ':' between two paths.extensions
must not have '.'.extensions
should end with ':' , also have ':' between two extensions. eg. /sdcard:/mnt:
Copyright (C) 2019 FilePicker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.