Get absolute path from Uri in Android 10

Mehul Kanzariya :

I am trying to integrate Imebra library to load .dcm files inside the app. The problem is that as per the documentation, I need to pass the absolute path of the file to Imebra as shown below:

val loadDataSet = CodecFactory.load("myFile.dcm")

For opening the DCM files, I am using the below code:

 val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
                addCategory(Intent.CATEGORY_OPENABLE)
                type = "*/*"
                putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
            }

            startActivityForResult(intent, RC_OPEN_FILES)

I am able to get the list of URI's for all the selected files using the below code:

 if (data != null) {
                val clipData = data.clipData
                if (clipData != null) {
                    // Multiple files selected
                    val clipDataUriList = arrayListOf<Uri>()
                    for (i in 0 until clipData.itemCount) {
                        clipDataUriList.add(clipData.getItemAt(i).uri)
                    }
                    processDcmFiles(clipDataUriList)
                } else {
                    // Single file selected
                    data.data?.let { processDcmFiles(arrayListOf(it)) }
                }

            }

I tried using uri.getPath() and creating a File using the URI and then getting the absolute path but none of them seems to work.

I am not sure if this the right approach to get the absolute path of the files in Android 10. Any help will be appreciated.

Paolo Brandoli :

This sample app uses the file selector to open and display a file: https://github.com/binarno/Imebra-V5-Android-Simple-Dicom-Viewer

The app delegates the opening and loading of the file (if necessary) to Android, then passes the file to Imebra via the Imebra Pipes.

While slightly more complex, it allows Imebra to read files also from external sources (e.g. Google Drive).

Disclaimer: I'm the author of Imebra

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174334&siteId=1