Android 笔记
2019-3-29
Uri、File、String 转换
val uri : Uri
val str = uri.toString()
val file = File(URI(str))
val uri = Uri.fromFile(file)
val uri = Uri.parse(str)
// content uri -> file path
private fun filePathFromContentUri(context: Context, uri: Uri): String?{
val pathColumn = Array(1){MediaStore.MediaColumns.DATA}
val cursor = context.contentResolver.query(uri, pathColumn, null, null, null)
cursor?.moveToFirst()
val columnIndex = cursor?.getColumnIndex(pathColumn[0])
val filePath =if(columnIndex ==null)null else cursor.getString(columnIndex)
cursor?.close()
return filePath
}