Effect of the demo
FileSelector.gif
GitHub source code
introduce
FileSelectorView is a custom file selector, based on which users can customize the file selector style.
function
- Switch directory
- Access path
- File filter
- File sorting
- Customize file ICONS and set sizes
- Set the size and color of the file name text
- Listen for the selected file
use
FileSelectorView is easy to use, just add it to the layout file, no other restrictions. For Android Studio users, you can add:
The compile 'com. Hz. Android. Fileselectorview: library: 1.3'Copy the code
- Layout file
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
......
<com.hz.android.fileselector.FileSelectorView
android:id="@+id/file_selector_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
......
</android.support.constraint.ConstraintLayout
Copy the code
- In the code
. / / switch fileSelectorView setCurrentDirectory (new File (Environment. External.getexternalstoragedirectory (), "Download")); / / Settings file filtering fileSelectorView. SetFileExtensionForFileFilter (arrays.aslist (" SHP ", "TXT")); / / custom File icon fileSelectorView. SetFileIconFactory (new FileIconCreator () {public Drawable getIcon (File File) {if (the File = = null) { return getResources().getDrawable(R.drawable.rotating); } else { return getResources().getDrawable(R.drawable.layers3); }}}); fileSelectorView.setTextSize(30); / / set the text size fileSelectorView setTextColor (Color. GREEN); / / set the text color fileSelectorView. SetIconSize (200); / / set the icon size is set to place the size of the icon of the imageView / / set. Select the file to monitor fileSelectorView setFileSelectedListener (new FileSelectorView.OnFileSelectedListener() { @Override public void onSelected(File selectedFile) { Toast.makeText(MainActivity.this, "" + selectedFile.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } @Override public void onFilePathChanged(File file) { curPathTextView.setText(file.getAbsolutePath()); }}); / / Settings file sorting fileSelectorView. SetFileSortComparator (new fileSelectorView. FileAscSortComparator ()); }...Copy the code
Pay attention to
Reading the file path requires permission from the user:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code