Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kingvhit opened this issue Nov 27, 2019 · 1 comment
Open

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

kingvhit opened this issue Nov 27, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@kingvhit
Copy link

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
        }
  1. 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 rosuH added the enhancement New feature or request label Nov 28, 2019
@rosuH
Copy link
Owner

rosuH commented Nov 28, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants