I am trying to use MaterialFilePicker for my Android Java music player. I have included it in my two build.gradle
files, and have adjusted my colors.xml
and styles.xml
files accordingly, but can't figure out where to go next. The readme.md
file in the GitHub repository told me this:
Open your class and add the following code
And provided me with this code:
...
public static final int FILE_PICKER_REQUEST_CODE = 989
...
MaterialFilePicker()
// Pass a source of context. Can be:
// .withActivity(Activity activity)
// .withFragment(Fragment fragment)
// .withSupportFragment(androidx.fragment.app.Fragment fragment)
.withActivity(this)
// With cross icon on the right side of toolbar for closing picker straight away
.withCloseMenu(true)
// Entry point path (user will start from it)
.withPath(alarmsFolder.absolutePath)
// Root path (user won't be able to come higher than it)
.withRootPath(externalStorage.absolutePath)
// Showing hidden files
.withHiddenFiles(true)
// Want to choose only jpg images
.withFilter(Pattern.compile(".*\\.(jpg|jpeg)$"))
// Don't apply filter to directories names
.withFilterDirectories(false)
.withTitle("Sample title")
.withRequestCode(FILE_PICKER_REQUEST_CODE)
.start()
...
But I can't figure out how do implement this code.
I have created a file called MaterialFilePicker.java
and have modified the code provided to look like this:
package com.infinityfreeapp.formalsoftware.audioplayer;
import java.util.regex.Pattern;
import MaterialFilePicker;
public class MaterialFilePicker {
public static final int FILE_PICKER_REQUEST_CODE = 989;
MaterialFilePicker() {
// Pass a source of context. Can be:
// .withActivity(Activity activity)
// .withFragment(Fragment fragment)
// .withSupportFragment(androidx.fragment.app.Fragment fragment)
.withActivity(this)
// With cross icon on the right side of toolbar for closing picker straight away
.withCloseMenu(true)
// Entry point path (user will start from it)
.withPath(alarmsFolder.absolutePath)
// Root path (user won't be able to come higher than it)
.withRootPath(externalStorage.absolutePath)
// Showing hidden files
.withHiddenFiles(true)
// Want to choose only jpg images
.withFilter(Pattern.compile(".*\\.(jpg|jpeg)$"))
// Don't apply filter to directories names
.withFilterDirectories(false)
.withTitle("Sample title")
.withRequestCode(FILE_PICKER_REQUEST_CODE)
.start();
}
}
But it is still riddled with errors.
Material File Picker seems outdated and not very well documented.
I would recommend storage chooser which seems much more documented and professional.
For example, to implement it, put this in your dependencies:
com.github.codekidX:storage-chooser:2.0.4.4
And then you can use it to pick files like this:
// Initialize Builder
StorageChooser chooser = new StorageChooser.Builder()
.withActivity(MainActivity.this)
.withFragmentManager(getFragmentManager())
.withMemoryBar(true)
.allowCustomPath(true)
.setType(StorageChooser.FILE_PICKER)
.build();
// Show dialog whenever you want by
chooser.show();
// get path that the user has chosen
chooser.setOnSelectListener(new StorageChooser.OnSelectListener() {
@Override
public void onSelect(String path) {
Log.e("SELECTED_PATH", path);
}
});