rosuH / AndroidFilePicker

FilePicker is a small and fast file selector library that is constantly evolving with the goal of rapid integration, high customization, and configurability~
https://afp.rosuh.me
MIT License
974 stars 85 forks source link

External Storage Supported (Sd-card, other externals ...) #56

Open kingvhit opened 4 years ago

kingvhit commented 4 years ago

Hi, Thank you very much for your contributions. I have to use your library with many customize and this is some point that I have made a change to my own.

This is some sample code that anyone can try

  1. FileUtils

        /**
         * 根据配置参数获取根目录文件
         * @return File
         */
        fun getRootFile(): File {
            return when (FilePickerManager.config.mediaStorageType) {
                STORAGE_EXTERNAL_STORAGE -> {
                    File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
                }
                STORAGE_CUSTOM_ROOT_PATH -> {
                    if (FilePickerManager.config.customRootPath.isEmpty()) {
                        File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
                    } else {
                        File(FilePickerManager.config.customRootPath)
                    }
                }
                else -> {
                    File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
                }
            }
        }
    
        /**
         * Function to get all external storage list
         */
        fun getExternalStorageList(context: Context): List<String> {
            val files = ContextCompat.getExternalFilesDirs(context, null)
            val externalFiles = mutableListOf<String>()
            for (i in files.indices) {
                val file = files[i]
                if (file != null && file.canRead()) {
                    val substring = file.absolutePath
                        .substring(0, file.absolutePath.indexOf("/Android/data/"))
                    externalFiles.add(substring)
                }
            }
            return externalFiles
        }
  2. Change when user tap to on item of Nav Header, then if they tap to first position (like root of the path), We will display the popup menu

    override fun onItemClick(
        recyclerAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>,
        view: View,
        position: Int
    ) {
        val item = (recyclerAdapter as me.rosuh.filepicker.adapter.BaseAdapter).getItem(position)
        item ?: return
        val file = File(item.filePath)
        if (!file.exists()) {
            return
        }
        when (view.id) {
            R.id.item_list_file_picker -> {
                if (file.isDirectory) {
                    (rv_nav_file_picker?.adapter as? FileNavAdapter)?.let {
                        saveCurrPos(it.data.last(), position)
                    }
                    // 如果是文件夹,则进入
                    enterDirAndUpdateUI(item)
                } else {
                    // Navigate data to activity
                    FilePickerManager.config.fileItemOnClickListener.onItemClick(
                        recyclerAdapter,
                        view,
                        position
                    )
                }
            }
            R.id.item_nav_file_picker -> {
                if (file.isDirectory) {
                    // In-case this is the root of path
                    if (position == 0 && strExternalStoragePaths.size > 1) {
                        // Display the popup that the user can switch between external storage like SD-card, internal storage ...
                        /* Creating the instance of PopupMenu */
                        popup = PopupMenu(
                            requireContext(),
                            (rv_nav_file_picker?.layoutManager as? LinearLayoutManager)?.findViewByPosition(
                                0
                            ) ?: rv_nav_file_picker
                        )

                        for (index in strExternalStoragePaths.indices) {
                            popup?.menu?.add(
                                0,
                                index,
                                index,
                                if (index == 0) R.string.file_picker_tv_internal_storage else R.string.file_picker_tv_sd_card
                            )
                        }

                        // Set handle event click
                        popup?.setOnMenuItemClickListener { item ->
                            /*
                             * In-case data is invalid
                             */
                            if (item.itemId < 0 || item.itemId >= strExternalStoragePaths.size) {
                                return@setOnMenuItemClickListener false
                            }
                            // Get new path of selected menu item
                            val strNewPath = strExternalStoragePaths[item.itemId]
                            // In-case the path is null
                            if (TextUtils.isEmpty(strNewPath)) {
                                return@setOnMenuItemClickListener false
                            }
                            // Handle UI/ update view after get new root path
                            pickerConfig.setCustomRootPath(strNewPath)

                            // Clear data and re-load list
                            navDataSource.clear()
                            // Reload list.
                            loadList()
                            true
                        }
                        popup?.show()
                    } else {
                        navAdapter?.let {
                            saveCurrPos(it.data.last(), position)
                        }
                        // 如果是文件夹,则进入
                        enterDirAndUpdateUI(item)
                    }
                }
            }
        }
    }

All rights, you can implement it for your own caused my code is changed so much so I can't publish the sample.

The video below is demoed my code.

device-2019-11-28-001403.webm.zip

rosuH commented 4 years ago

Thank you for your contribution. Those features are awesome! 👍 I will consider adding these features as appropriate. Thanks again! ❤️