I am using the android file picker to select multiple files from external storage, but the Uri I'm getting doesn't have the correct name, it's like content://com.android.providers.media.documents/document/image%3A2
image%3A25 is not the correct name of the file and it is also missing the extension.
Following is my code:
val selectFilesActivityResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
//If multiple files selected
if (data?.clipData != null) {
val count = data.clipData?.itemCount ?: 0
for (i in 0 until count) {
fileUtils.importFile(
data.clipData?.getItemAt(i)?.uri!!,
viewModel.getInternalPath()
)
}
}
//If single file selected
else if (data?.data != null) {
fileUtils.importFile(
data.data!!,
viewModel.getInternalPath()
)
}
}
}
the file picker intent
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.type = "*/*"
selectFilesActivityResult.launch(intent)
What am I doing wrong?
I tried changing the Intent.ACTION_OPEN_DOCUMENT to Intent.ACTION_GET_CONTENT but it didn't work.
That number has nothing to do with a file name or extension or file path.
There is no name in that uri and its not needed too.
It is just an index for the content provider to use in its database to get the right file when you ask for it.
To get the name of the file -inclusive extension- you just have to query() that content provider for DISPLAY_NAME for the uri.