Cannot retrieve image Uri shared from Gallery (Xiaomi)

PressRex profile image
by PressRex
Cannot retrieve image Uri shared from Gallery (Xiaomi)

I am trying to retrieve an image Uri shared from a Gallery app by intent to show it in my application. But it doesn't work on Xiaomi API 33. (It works fine on my other devices: Pixel API 35, Huawei API 29).

This is my code:

fun resolveUri(intentUri: Uri) {
    val projection = arrayOf(MediaStore.Images.Media._ID)

    contentResolver.query(
        intentUri,
        projection,
        null,
        null,
        "${MediaStore.Images.Media.DATE_ADDED} DESC"
    )?.use { cursor ->
        val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID)
        while (cursor.moveToNext()) {
            val imageUri: Uri = Uri.withAppendedPath(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                cursor.getString(idColumn)
            )

            Log.d("TAG", "imageUri: $imageUri")
        }
    }
}

in result I am getting something like: photoUri: content://media/external/images/media/31.

The ID seems to be random and increased every time I am trying to share the same photo.

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks
Could not load content

Latest posts