There are a couple of solutions on how to get thumbnail from the full image, such as
android get thumbnail of image stored on sdcard whose path is known
However, I need on the contrary to receive full image Uri from a Thumbnail Uri (or thumbnail id).
Here is how I get thumbnails:
fun getGalleryImages(): List<LocalImage> {
val baseUri: Uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI
val listOfAllImages = ArrayList<LocalImage>()
// Set up an array of the Thumbnail Image ID column we want
val projection = arrayOf(MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID)
// Create the cursor pointing to the SDCard
val cursor = context.contentResolver.query(
baseUri,
projection,
null,
null,
null)
// Get the column index of the Thumbnails Image ID
val thumbColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID)
val fullColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID)
var thumbnailUri: Uri?
while (cursor.moveToNext()) {
val thumbId = cursor.getString(thumbColumnIndex)
thumbnailUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + thumbId)
// here I save image id for later retrieving full image
val imageId = cursor.getString(fullColumnIndex)
listOfAllImages.add(LocalImage(thumbnailUri = thumbnailUri), imId = imageId)
}
cursor.close()
return listOfAllImages
}
And then I have to retrieve a full image by image id (or by thumbnail Uri)
private fun getFullImage(imageId: String): Uri {
val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA)
val cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media._ID + "=?",
arrayOf(imageId),
null)
val columnIndex = cursor.getColumnIndex(projection[0])
if (cursor.moveToFirst()) {
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + cursor.getString(1))
}
cursor.close()
return Uri.EMPTY
}
This returns me a Uri which looks realistic:
content://media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg
However, the Uri seems to be invalid since I cannot retrieve the image from it:
val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, image.imageUri)
java.lang.IllegalStateException: Unknown URL: content://media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg at android.os.Parcel.readException(Parcel.java:1950) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146) at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
Picasso also fails to load image from this Uri