I want to select files and folders in the Android storage access framework

Viewed 20

I am developing an application in android. I am using the storage access framework, and I can select multiple files, but not folders. Is it not possible to select folders?

What we tried 1

Multiple file selections were possible, but not folder selections.

class MainFragment : Fragment() {
private val launcher = 
    registerForActivityResult(ActivityResultContracts.GetMultipleContents()){ result ->
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.materialButton.setOnClickListener {
            launcher.launch(null)
        }
    }
}

What we tried 2

Multiple selections and file selections were not available.

class MainFragment : Fragment() {
    private val launcher = 
        registerForActivityResult(ActivityResultContracts.OpenDocumentTree()){ result ->

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.materialButton.setOnClickListener {
            launcher.launch(null)
        }
    }
}
1 Answers

I am using the storage access framework, and I can select multiple files, but not folders.

I am assuming that you meant to say:

I am using the storage access framework, and I can select multiple files, but not multiple folders.

(emphasis showing added context)

If so, then yes, there is no multiple-tree ("folder") option the way there is a multiple-document option. The expectation is that the user would select some common parent between the trees of interest. From there, your app can present its own UI to allow users to work with specific sub-trees of the selected tree.

Related