I need to develop functionality to load an image from the gallery and upload it to the server.
The format that is sent to the server is File. When I select a picture from the phone, I get the Uri content.
The old way that was used to convert Uri content to full image path is:
fun getFilePathFromUri(context: Context, uri: Uri) : String? {
var realPath: String? = null
try {
val column = MediaStore.Images.Media.DATA
val projection = arrayOf(column)
context.contentResolver.query(
uri,
projection,
null,
null,
null
)?.use { cursor ->
if (cursor.count > 0) {
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndexOrThrow(column)
realPath = cursor.getString(columnIndex)
}
}
cursor.close()
}
} catch (e: Exception) {
Timber.e(e)
}
return realPath
}
Google has announced that now MediaStore.Images.Media.DATA -> 'DATA: String' is deprecated. How is this done now? How to get the full path?