I have this code where basically i got a file path ( not a media one ), for some reason, in some Android 10 devices I´m getting the path as
/document/msf:<number>
In the manifest I have
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
private val xfile = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
var xmlFile = FileWrapperFactory(requireContext()).getAssetFileWrapper("file")
var xmlPath = ""
if (result.resultCode == Activity.RESULT_OK) {
xmlPath = result.data?.data?.path.toString()
xmlFile = FileWrapperFactory(requireContext()).getExternalFileWrapper(xmlPath)
}
viewBinding.pathName.setText(xmlPath)
}
....
fun getFile() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
xfile.launch(Intent.createChooser(intent, "file"))
}
I need to change my code to that scenario, and I tried several solutions but no one succeeded.
0
Since your file type is not a media one, follow this official guide: Access documents and other files from shared storage
Update:
If you just need the URI, use this code:
private val readFile = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
uri?.let { documentUri ->
// use the URI here
}
}
fun readFileContent() {
val mimeTypes = arrayOf("*/*") // You can specify MIME types if needed
readFile.launch(mimeTypes)
}
2
Try add android:requestLegacyExternalStorage="true"
into AndroidManifest.xml file.