Android 文件選擇器,指定選擇文件夾還是文件,根據後綴名過濾,支持多選

Android 文件選擇器,指定選擇文件夾還是文件,根據後綴名過濾,支持多選

FileSelector 直通車


### 介紹

  • 可指定選擇文件夾還是文件,選擇顯示文件也可指定後綴名顯示獲取連接
  • 可通過掃描全盤的方式,選擇指定後綴名的文件
  • 支持多選。
  • 支持Activity、Fragment
     

### 示例圖

 

1234

 

### 用法

```
allprojects {
   repositories {
      ...
      maven { url 'https://www.jitpack.io' }
   }
}
```

```
implementation 'com.github.ZLYang110:FileSelector:1.0'
```



##### 一、 在文件瀏覽器中選擇指定文件
 
 /**
     *  設置 onlyShowFolder() 只顯示文件夾 後 再設置setFileTypes()不生效
     *  設置 onlyShowFolder() 只顯示文件夾 後 默認設置了onlySelectFolder()
     *  設置 onlySelectFolder() 只能選擇文件夾 後 默認設置了isSingle()
     *  設置 isSingle() 只能選擇一個 後 再設置了setMaxCount() 不生效
     *
     */
 FileSelector.from(this)
               // .onlyShowFolder()  //只顯示文件夾
                //.onlySelectFolder()  //只能選擇文件夾
               // .isSingle() // 只能選擇一個
                .setMaxCount(5) //設置最大選擇數
                .setFileTypes("png", "doc","apk", "mp3", "gif", "txt", "mp4", "zip") //設置文件類型
                .setSortType(FileSelector.BY_NAME_ASC) //設置名字排序
                //.setSortType(FileSelector.BY_TIME_ASC) //設置時間排序
                //.setSortType(FileSelector.BY_SIZE_DESC) //設置大小排序
                //.setSortType(FileSelector.BY_EXTENSION_DESC) //設置類型排序
                .requestCode(1) //設置返回碼
                .start();


##### 二、 設置只選擇文件夾(文件夾默認只能選擇一個)

 
 FileSelector.from(this)
                .onlySelectFolder()  //只能選擇文件夾
                .requestCode(1) //設置返回碼
                .start();

##### 三、 設置只顯示文件夾(只顯示文件夾就只能選擇文件夾)

 
 FileSelector.from(this)
                .onlyShowFolder()  //只能選擇文件夾
                .requestCode(1) //設置返回碼
                .start();


##### 三、 只顯示圖片的文件

 
 FileSelector.from(this)
                .setMaxCount(5) //設置最大選擇數
                .setFileTypes( "png","jpg") //設置文件類型
                .requestCode(1) //設置返回碼
                .start();


##### 四、 接收返回的文件數據,在 ++onActivityResult++ 方法中獲取。選中文件以鏈表方式返回, ++EssFile++ 類爲載體



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       if (resultCode == RESULT_OK) {
                   if (requestCode == 1) {
                       ArrayList<String> essFileList = data.getStringArrayListExtra(Const.EXTRA_RESULT_SELECTION);
                       StringBuilder builder = new StringBuilder();
                       for (String file :
                               essFileList) {
                           builder.append(file).append("\n");
                       }
                       tv_backResult.setText(builder.toString());
                   }
               }
    }

### 屬性列表

---

名稱 | 描述 |  默認值
---|---|---
FileTypes | 需要顯示的文件類型 | 無
SortType | 排序類型 | 按名字排序 BY_NAME_ASC
isSingle | 是否單選 |false
maxCount | 最大可選中數量 | 10
request_code | 請求碼 | 無
onlyShowFolder | 是否僅只顯示文件夾  | false
onlySelectFolder | 是否只選擇文件夾  | false


### THANKS

---

[陳宇明大師兄 BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper)

[FilePicker](https://github.com/imLibo/FilePicker)


## LICENSE

MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章