Sunday 7 May 2023

How do I use MaterialFilePicker?

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.



from How do I use MaterialFilePicker?

No comments:

Post a Comment