Android文件路径选择器
提供文件或路径选择,自动申请存储权限,支持安卓4.4 ~ 12,适配Android11分区存储特性,支持Android/data目录访问,支持自定义UI,支持SD卡。(Provide file or path selection, automatically apply for storage permission, support Android 4.4 to 12, support Android/data directory access, support custom UI,Support SD card.The Keyword:file selector operator android/data android 11)
前言
在开始之前可以给我一个小星星吗?
非常感谢,你的支持是我唯一动力。
欢迎Star和Issues!
项目地址:
语言(Language)
一、如何使用
第1步:添加仓库:
-
如果你的项目 Gradle 配置是在 7.0 以下 ,需要在 build.gradle 文件中加入
allprojects {
repositories {
...
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
-
如果你的 Gradle 配置是 7.0 及以上 ,则需要在 settings.gradle 文件中加入
dependencyResolutionManagement {
repositories {
...
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
第2步:添加远程依赖:
-
配置完远程仓库后,在项目 app 模块下的 build.gradle 文件中加入远程依赖 -
最新发布版:请前往项目Github地址查看版本号:传送门
dependencies {
...
implementation 'io.github.molihuan:pathselector:版本'
}
第3步:基本用法示范:
PathSelector.build(MainActivity.this, Constants.BUILD_ACTIVITY)
.requestCode(10011)
.setMoreOPtions(new String[]{"选择"},
new boolean[]{true},
new SelectOptions.onToolbarOptionsListener() {
@Override
public void onOptionClick(View view, String currentPath, List<FileBean> fileBeanList, List<String> callBackData, TabbarFileListAdapter tabbarAdapter, FileListAdapter fileAdapter, List<FileBean> callBackFileBeanList) {
}
}
)
.start();
第4步:获取返回的数据(也可以在点击回调中获取数据):
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10011) {
if(data!=null){
List<String> pathData = data.getStringArrayListExtra(Constants.CALLBACK_DATA_ARRAYLIST_STRING);
StringBuilder builder = new StringBuilder();
for (String path : pathData) {
builder.append(path).append("");
}
Mtools.toast(MainActivity.this,builder.toString());
}
}
}
demo演示:
系统版本:Android 11
二、更多设置
1、Activity&所有设置模式:
PathSelector.build(MainActivity.this, Constants.BUILD_ACTIVITY)
.requestCode(10011)
.setRootPath("/storage/emulated/0")
.setMaxCount(3)
.setSortType(Constants.SORT_NAME_ASC)
.showToolBarFragment(true)
.setToolbarMainTitle("路径选择器")
.setToolbarSubtitleTitle("MLH")
.setToolbarSubtitleColor(Color.BLACK)
.setMoreChooseItems(
new String[]{"全选", "删除"},
new SelectOptions.onMoreChooseItemsListener() {
@Override
public void onItemsClick(View view, String currentPath, List<FileBean> fileBeanList, List<String> callBackData, TabbarFileListAdapter tabbarAdapter, FileListAdapter fileAdapter, List<FileBean> callBackFileBeanList) {
Mtools.toast(getBaseContext(),"点击了全选");
}
},
(view,currentPath, fileBeanList,callBackData,tabbarAdapter,fileAdapter,callBackFileBeanList) -> {
Mtools.toast(getBaseContext(),"点击了删除");
}
)
.setMoreOPtions(
new String[]{"选择"},
new boolean[]{false},
(view,currentPath, fileBeanList,callBackData,tabbarAdapter,fileAdapter,callBackFileBeanList) -> {
Mtools.toast(getBaseContext(),"点击了选择"+callBackData.get(0));
}
)
.setFileItemListener(new com.molihuan.pathselector.dao.SelectOptions.onFileItemListener() {
@Override
public boolean onFileItemClick(View view, String currentPath, List<com.molihuan.pathselector.entities.FileBean> fileBeanList, List<String> callBackData, TabbarFileListAdapter tabbarAdapter, com.molihuan.pathselector.adapters.FileListAdapter fileAdapter,FileBean fileBean) {
Mtools.toast(getBaseContext(),currentPath);
return false;
}
@Override
public boolean onLongFileItemClick(View view, String currentPath, List<com.molihuan.pathselector.entities.FileBean> fileBeanList, List<String> callBackData, TabbarFileListAdapter tabbarAdapter, com.molihuan.pathselector.adapters.FileListAdapter fileAdapter,FileBean fileBean) {
return false;
}
})
.start();
2、Fragment模式:
mPathSelectFragment = PathSelector.build(MainActivity.this, Constants.BUILD_FRAGMENT)
.frameLayoutID(R.id.fragment_select_show_area)
.requestCode(10011)
.showToolBarFragment(false)
.setMoreChooseItems(
new String[]{"全选", "删除"},
(view,currentPath, fileBeanList,callBackData,tabbarAdapter,fileAdapter,callBackFileBeanList) -> {
Mtools.toast(getBaseContext(),"点击了全选");
},
(view,currentPath, fileBeanList,callBackData,tabbarAdapter,fileAdapter,callBackFileBeanList) -> {
Mtools.toast(getBaseContext(),"点击了删除"+callBackData.get(0));
}
)
.start();
onBackPressed()中
@Override
public void onBackPressed() {
if (mPathSelectFragment!=null&&mPathSelectFragment.onBackPressed()){
return;
}
super.onBackPressed();
}
3、Dialog模式:
mPathSelectFragment = PathSelector.build(MainActivity.this, Constants.BUILD_DIALOG)
.frameLayoutID(R.id.fragment_select_show_area)
.requestCode(10011)
.showToolBarFragment(true)
.setMoreOPtions(
new String[]{"选择"},
new boolean[]{true},
(view,currentPath, fileBeanList,callBackData,tabbarAdapter,fileAdapter,callBackFileBeanList) -> {
Mtools.toast(getBaseContext(),"点击了选择"+currentPath);
}
)
.start();
4、自定义Toolbar:
mPathSelectFragment = PathSelector.build(MainActivity.this, Constants.BUILD_ACTIVITY)
.requestCode(10011)
.showToolBarFragment(true)
.setToolbarFragment(new CustomToolbarFragment())
.setToolbarViewClickers(
new SelectOptions.onToolbarListener() {
@Override
public void onClick(View view, String currentPath, List<FileBean> fileBeanList, List<String> callBackData, TabbarFileListAdapter tabbarAdapter, FileListAdapter fileAdapter,List<FileBean> callBackFileBeanList) {
Mtools.toast(getBaseContext(),"点击了按钮1");
}
}
)
.start();
三、类与方法(尽量看源码,都写了注释,懒得写文档)
SelectManager
方法 | 注释 | 备注 |
---|
frameLayoutID(int id) | 设置加载位置FrameLayoutID | | requestCode(int code) | 设置请求码 | | setRootPath(String path) | 设置开始默认路径 | | setMaxCount(int maxCount) | 设置最大选择数量 | 不设置默认为-1 即无限 | setShowFileTypes(String… fileTypes) | 设置显示文件类型 | | setSelectFileTypes(String… fileTypes) | 设置选择文件类型 | | setSortType(int sortType) | 设置排序规则 | | isSingle() | 设置单选 | | showToolBarFragment(boolean var) | 设置ToolBarFragment是否显示 | | setMoreOPtions(String[] optionsName ,SelectOptions.onToolbarOptionsListener …optionListeners) | 设置一些Toolbar选项(有重载) | | setToolbarViewClickers(SelectOptions.onToolbarListener …listeners) | 设置一些Toolbar点击 | | SelectManager setMoreChooseItems(String[] ItemsName ,SelectOptions.onMoreChooseItemsListener …itemListeners) | 设置一些MoreChooseItems选项(有重载) | | setFileItemListener(SelectOptions.onFileItemListener onFileItem) | FileItem点击/长按回调 | | setToolbarMainTitle(String title) | 设置Toolbar主标题 | 标题相关选项一系列不一一列举 | setToolbarFragment(Fragment fragment) | 设置自定义标题栏UI | 建议继承AbstractToolbarFragment | setMoreChooseFragment(Fragment fragment) | 设置自定义多选UI | | start() | 开始构建 | 必须调用 | … | … | |
四、!!!特别注意!!!
分区存储
该库以及适配了分区存储,不需要额外适配,你只需要写你的业务代码即可,其他的交给它。
-
注意该库已经在库的AndroidManifest.xml 中添加了:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:preserveLegacyExternalStorage="true"
android:requestLegacyExternalStorage="true"
>
-
可能会报错: Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed with multiple errors, see logs
请在你的项目中的AndroidManifest.xml 设置一致
非常感谢
ZLYang110/FileSelector: Android 文件选择器,指定选择文件夹还是文件,根据后缀名过滤,支持多选 (github.com)
zzy0516alex/FileSelectorRelease: lib (github.com)
getActivity/XXPermissions: Android 权限请求框架,已适配 Android 12 (github.com)
CymChad/BaseRecyclerViewAdapterHelper: BRVAH:Powerful and flexible RecyclerAdapter (github.com)
Blankj/AndroidUtilCode: Android developers should collect the following utils(updating). (github.com)
开源项目以及其依赖项目。
|