android file selector component

Keywords: Android github

demo effect

FileSelector.gif

Source code on GitHub

introduce

FileSelectorView is a custom file selector, based on which users can customize the style of file selector.

function
  • Switch directories
  • Get path
  • File filtering
  • File sorting
  • Custom file icon and set size
  • Set the size and color of file name text
  • Listen for selected files
Use

FileSelectorView is easy to use, just add it to the layout file, no other restrictions.
For Android Studio users, add:

compile 'com.hz.android.fileselectorview:library:1.1'
  • 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   

  • In code
......

    //Switch directories
    fileSelectorView.setCurrentDirectory(new File(Environment.getExternalStorageDirectory(), "Download"));
    //Set file filtering
    fileSelectorView.setFileExtensionForFileFilter(Arrays.asList("shp", "txt"));
    //Custom file Icon
    fileSelectorView.setFileIconFactory(new FileIconCreator() {
        public Drawable getIcon(File file) {
            if (file == null) {
                return getResources().getDrawable(R.drawable.rotating);
            } else {
                return getResources().getDrawable(R.drawable.layers3);
            }
        }
    });

    fileSelectorView.setTextSize(30);//Set text size
    fileSelectorView.setTextColor(Color.GREEN); //Set text color
    fileSelectorView.setIconSize(200); //Setting the icon size is to set the size of the imageView where the icon is placed

    //Set listening of selection file
    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());
        }
    });
    
    //Set file sorting
    fileSelectorView.setFileSortComparator(new FileSelectorView.FileAscSortComparator());
}
    
    ......


Be careful

The user's permission is required to read the file path:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Posted by liebs19 on Tue, 05 May 2020 07:40:02 -0700