반응형

 

fun getImageContentUri(path : String) : Uri? {

    val cursor = context?.contentResolver?.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    ,  arrayOf(MediaStore.Images.Media._ID)
    , MediaStore.Images.Media.DATA+"=? "
    , arrayOf(path)
    , null
    )

    if (cursor != null && cursor.moveToFirst()) {

        val idx = cursor.getColumnIndex(MediaStore.MediaColumns._ID)
        val id = cursor.getInt(idx)
        return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , id.toString())

    }  else if (path.isNotEmpty()) {

        val values = ContentValues()
        values.put(MediaStore.Images.Media.DATA , path)
        return context?.contentResolver?.insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values
        )
    }

    return null
}

 

반응형

+ Recent posts