Last active
January 13, 2022 07:06
-
-
Save Morteza-QN/40475756986fb9f1a1398d6cb42e00cd to your computer and use it in GitHub Desktop.
Function convert Uri content to real path file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun convertUriToPath(context: Context, uri: Uri?): String { | |
var filePath = "" | |
val wholeID = DocumentsContract.getDocumentId(uri) | |
// Split at colon, use second item in the array | |
val id = wholeID.split(":").toTypedArray()[1] | |
val column = arrayOf(MediaStore.Images.Media.DATA) | |
// where id is equal to | |
val sel = MediaStore.Images.Media._ID + "=?" | |
val cursor = context.contentResolver | |
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, arrayOf(id), null) | |
val columnIndex = cursor!!.getColumnIndex(column[0]) | |
if (cursor.moveToFirst()) { | |
filePath = cursor.getString(columnIndex) | |
} | |
cursor.close() | |
return filePath | |
} | |
fun Uri.toFilePath(context: Context): String { | |
var filePath = "" | |
val wholeID = DocumentsContract.getDocumentId(this) | |
// Split at colon, use second item in the array | |
val id = wholeID.split(":").toTypedArray()[1] | |
val column = arrayOf(MediaStore.Images.Media.DATA) | |
// where id is equal to | |
val sel = MediaStore.Images.Media._ID + "=?" | |
val cursor = context.contentResolver | |
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, arrayOf(id), null) | |
val columnIndex = cursor!!.getColumnIndex(column[0]) | |
if (cursor.moveToFirst()) { | |
filePath = cursor.getString(columnIndex) | |
} | |
cursor.close() | |
return filePath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment