Banner

mlhfileselector

[![Maven Central](https://img.shields.io/maven-central/v/io.github.molihuan/pathselector.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.molihuan%22%20AND%20a:%22pathselector%22)[![jitpack](https://jitpack.io/v/molihuan/mlhfileselectorlib.svg)](https://jitpack.io/#molihuan/mlhfileselectorlib)![API: 19-33 (shields.io)](https://img.shields.io/badge/API-19--33-green)[![license: Apache-2.0 (shields.io)](https://img.shields.io/badge/license-Apache--2.0-brightgreen)](https://github.com/molihuan/mlhfileselectorlib/blob/master/LICENSE)[![Star](https://img.shields.io/github/stars/molihuan/mlhfileselectorlib.svg)](https://github.com/molihuan/mlhfileselectorlib)[![bilibili: 玲莫利 (shields.io)](https://img.shields.io/badge/bilibili-玲莫利-orange)](https://space.bilibili.com/454222981)[![CSDN: molihuan (shields.io)](https://img.shields.io/badge/CSDN-molihuan-blue)](https://blog.csdn.net/molihuan)

Library that provides file or path selection on Android

Automatically apply for storage permission, support Android 4.4 to 13, support Android/data and Android/obb directory access,

support custom UI,support SD card.

(The Keyword:file selector operator android/data android 11 android 13)

## Language(语言) #### **[Chinese](./README.md)** | [English](./README_EN.md) ## Why choose me? Automatically apply storage permission, support Android4.4 ~ 13, no longer need to struggle to adapt to various versions, fast integration, a code to get it done, perfect documentation, support no root access and operation of Android/data and Android/obb directory (adapted to Android 13), support SD card, highly customizable UI to meet all your needs, use Very flexible, support internationalization, for Android file selection you just need to focus on your business code and leave the rest to it. ## Characteristics - [x] Automatically request storage permissions(Can control) - [x] Android 4.4 ~ 13 - [x] Android/data and Android/obb directory access and manipulation - [x] SD Card - [x] Highly customizable UI - [x] Internationalization - [ ] Search function - [x] Custom icon - [ ] Show hidden files ## Preface #### Can you give the project a Star before starting? Thank you very much, your support is the only thing that keeps me going. Welcome Star and Issues! #### We need your Pr. Note (please Pr to dev branch) #### Project Address: ##### [Github](https://github.com/molihuan/mlhfileselectorlib) ##### [Gitee](https://gitee.com/molihuan/mlhfileselectorlib) ## Demo: #### Android version:Android 13 #### Download Links:[Experience App](https://github.com/molihuan/mlhfileselectorlib/tree/master/app/release) ![pathSelectorDemo.gif](https://s2.loli.net/2022/12/14/QSGrIvwzYKhZuMe.gif) ## I. Quick start #### Step 1: Add the repository: - ##### If your project Gradle configuration is under `7.0`, you need to add to the `build.gradle` file ```java allprojects { repositories { ... mavenCentral() maven { url 'https://jitpack.io' } } } ``` - ##### If your Gradle configuration is `7.0 and above`, you need to add to your `settings.gradle` file ```java dependencyResolutionManagement { repositories { ... mavenCentral() maven { url 'https://jitpack.io' } } } ``` #### Step 2: Add the remote dependency: - ##### After configuring the remote repository, add the remote dependency to the `build.gradle` file under the project app module - ##### Latest Release:[![Maven Central](https://img.shields.io/maven-central/v/io.github.molihuan/pathselector.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.molihuan%22%20AND%20a:%22pathselector%22) ```java dependencies { ... // We recommend using the latest release version, please see above for the latest release version // Please replace "version" with a specific version number, e.g. 1.1.16 implementation 'io.github.molihuan:pathselector:version' } ``` #### Step 3: Demonstration of basic usage: ```java //Permissions will be requested automatically if you don't have them PathSelector.build(this, MConstants.BUILD_DIALOG)//Dialog build mode .setMorePopupItemListeners( new CommonItemListener("OK") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { /**Dialog dismiss * pathSelectFragment.close(); */ StringBuilder builder = new StringBuilder(); builder.append("you selected:\n"); for (FileBean fileBean : selectedFiles) { builder.append(fileBean.getPath() + "\n"); } Mtools.toast(builder.toString()); return false; } } ) .show();//Start building ``` ## II. Basic settings #### Turn on debug mode ```java //Turn on debug mode, please turn off production environment PathSelectorConfig.setDebug(true); //or use PathSelector.setDebug(true); ``` #### 1、Activity build mode: ```java //Activity build mode PathSelectFragment selector = PathSelector.build(this, MConstants.BUILD_ACTIVITY) .setRequestCode(635) .setMorePopupItemListeners( new CommonItemListener("OK") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { StringBuilder builder = new StringBuilder(); builder.append("you selected:\n"); for (FileBean fileBean : selectedFiles) { builder.append(fileBean.getPath() + "\n"); } Mtools.toast(builder.toString()); return false; } } ) .show(); ``` #### 2、Fragment build mode: ##### Step 1: Use FrameLayout placeholders in the xml of the layout file you need to display ```xml ``` ##### Step 2: Write the code ```java //Get the PathSelectFragment instance and then handle the back button click event in onBackPressed PathSelectFragment selector = PathSelector.build(this, MConstants.BUILD_FRAGMENT) .setFrameLayoutId(R.id.fragment_select_show_area)//Load position, ID of FrameLayout .setMorePopupItemListeners( new CommonItemListener("OK") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { StringBuilder builder = new StringBuilder(); builder.append("you selected:\n"); for (FileBean fileBean : selectedFiles) { builder.append(fileBean.getPath() + "\n"); } Mtools.toast(builder.toString()); return false; } } ) .show(); ``` ##### Step 3: Override the onBackPressed() method to let the path selector take precedence over the return button click event
Very important!!!
Very important!!!
Very important!!!
##### Important things are to be repeated for 3 times ```java @Override public void onBackPressed() { //Let PathSelectFragment handle the return button click event first if (selector != null && selector.onBackPressed()) { return; } ...... super.onBackPressed(); } ``` #### 3、Dialog build mode & common settings. ```java //Get the PathSelectFragment instance and then handle the back button click event in onBackPressed PathSelectFragment selector = PathSelector.build(this, MConstants.BUILD_DIALOG) //.setBuildType(MConstants.BUILD_DIALOG)//Already set in the build //.setContext(this)//Already set in the build .setRootPath("/storage/emulated/0/")//Initial path .setShowSelectStorageBtn(true)//Whether to display internal storage selection button .setShowTitlebarFragment(true)//Whether to display the title bar .setShowTabbarFragment(true)//Whether to show breadcrumbs .setAlwaysShowHandleFragment(true)//Whether to always show the long press pop-up option .setShowFileTypes("", "mp3", "mp4")//Show only files with (no suffix) or (mp3 suffix) or (mp4 suffix) .setSelectFileTypes("", "mp3")//Only files with (no suffix) or (mp3 suffix) can be selected .setMaxCount(3)//You can select up to 3 files. The default is - 1 unlimited .setRadio()//Single choice(Use setMaxCount(0) to replace it if you need a single-selected folder) .setSortType(MConstants.SORT_NAME_ASC)//Sort by name .setTitlebarMainTitle(new FontBean("My Selector"))//Set the title bar main title, you can also set the font size, color, etc. .setTitlebarBG(Color.GREEN)//Set the title bar background color .setFileItemListener(//Set the callback for the file item click (it will be called back only if it is a file, but not if it is a folder) new FileItemListener() { @Override public boolean onClick(View v, FileBean file, String currentPath, BasePathSelectFragment pathSelectFragment) { Mtools.toast("you clicked path:\n" + file.getPath()); return false; } } ) .setMorePopupItemListeners(//Set the top right option callback new CommonItemListener("SelectAll") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { pathSelectFragment.selectAllFile(true); return false; } }, new CommonItemListener("DeselectAll") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { pathSelectFragment.selectAllFile(false); return false; } } ) .setHandleItemListeners(//Set long press pop-up option callback new CommonItemListener("OK") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { StringBuilder builder = new StringBuilder(); builder.append("you selected:\n"); for (FileBean fileBean : selectedFiles) { builder.append(fileBean.getPath() + "\n"); } Mtools.toast(builder.toString()); return false; } }, new CommonItemListener("cancel") { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { pathSelectFragment.openCloseMultipleMode(false); return false; } } ) .show(); ``` ## III. Advanced settings (custom UI) #### UI: ![UiLayout.png](https://s2.loli.net/2022/12/14/Yw8bamgrtNC9yiW.png) #### 1、custom option style (HandleItem as an example) ##### Way 1:Set the style by FontBean ```java PathSelectFragment selector = PathSelector.build(this, MConstants.BUILD_DIALOG) .setHandleItemListeners(//Set long press pop-up option callback //FontBean can set the text, the size of the word, the color of the word, and the icon to the left of the word //R.drawable.ic_test_mlh is your own image resource id new CommonItemListener(new FontBean("OK", 18, Color.RED, R.drawable.ic_test_mlh)) { @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { Mtools.toast("You Click"); return false; } } ) .show(); ```
What? This way is not enough for you, then try way 2
##### Way 2: Override CommonItemListener's setViewStyle method to customize the style ```java PathSelectFragment selector = PathSelector.build(this, MConstants.BUILD_DIALOG) .setHandleItemListeners( //Override CommonItemListener's setViewStyle method to customize the style new CommonItemListener("OK") { @Override public boolean setViewStyle(RelativeLayout container, ImageView leftImg, TextView textView) { textView.setTextSize(18); textView.setTextColor(Color.RED); //Icons are not displayed by default leftImg.setVisibility(View.VISIBLE); leftImg.setImageResource(R.drawable.ic_test_mlh); leftImg.getLayoutParams().width = 90; leftImg.getLayoutParams().height = 90; return true; } @Override public boolean onClick(View v, TextView tv, List selectedFiles, String currentPath, BasePathSelectFragment pathSelectFragment) { Mtools.toast("You Click"); return false; } } ) .show(); ```

What? What? This way is not enough for you, then you write the UI it to help you add, try the highly customizable UI

#### 2、Highly customizable UI (using Titlebar as an example): ##### Step 1: Create a new layout file, such as:fragment_custom_titlebar.xml ```xml