Arthurghev
Arthurghev

Reputation: 140

How to get the names of all files in a directory programmatically

I am trying to get all files from directory, but i am getting all file types except from .zip, .apk.

   val fileDir = File(folder)
   val files = fileDir.list() ?: return null

I don't know how to solve this problem. Can anyone help me?

Upvotes: 1

Views: 884

Answers (1)

Sidharth Mudgil
Sidharth Mudgil

Reputation: 1461

You can access the entire directory tree using the code below

Kotlin

fun openDirectory(pickerInitialUri: Uri) {
    // Choose a directory using the system's file picker.
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, your-request-code)
}

Java

public void openDirectory(Uri uriToLoad) {
    // Choose a directory using the system's file picker.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

    // Optionally, specify a URI for the directory that should be opened in
    // the system file picker when it loads.
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);

    startActivityForResult(intent, your-request-code);
}

to learn more, checkout

Upvotes: 1

Related Questions