MediaStore.Images从拇指Uri / id获取完整图像

关于如何从整个图像中获取缩略图,有几个解决方案,比如
android获取存储在路径已知的SD卡上的图像的缩略图

但是, 我需要相反,从缩略图Uri(或缩略图ID)接收完整的图像Uri
以下是我如何获得缩略图:

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 } 

然后我必须通过图像ID(或通过缩略图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 } 

这给我一个看起来很现实的Uri:

 content://media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg 

但是,由于我无法从中检索图像,Uri似乎无效:

 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)在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)在android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)在android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)

毕加索也无法加载这个Uri的图像

在获得图像的Uri后添加此代码。

 if (largeImagePath != null) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = OG; thumbnail = BitmapFactory.decodeFile((largeImagePath), opts); System.gc(); if (thumbnail != null) { try { thumbnail = Common.rotateImageIfRequired(mContext, thumbnail, Uri.fromFile(new File(largeImagePath))); } catch (IOException e) { e.printStackTrace(); } imageCam(thumbnail); } } public void imageCam(Bitmap thumbnail) { Bitmap photo = thumbnail; ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, bos); byte b[] = bos.toByteArray(); encodedImage = Base64.encodeToString(b, Base64.DEFAULT); ll_preview.setVisibility(View.VISIBLE); img_preview.setVisibility(View.VISIBLE); img_preview.setImageBitmap(photo); } 

在Common.java文件中添加这个方法。

  /** * Rotate an image if required. * * @param img The image bitmap * @param selectedImage Image URI * @return The resulted Bitmap after manipulation */ public static Bitmap rotateImageIfRequired(Context mContext, Bitmap img, Uri selectedImage) throws IOException { InputStream input = mContext.getContentResolver().openInputStream(selectedImage); ExifInterface ei; if (Build.VERSION.SDK_INT > 23) ei = new ExifInterface(input); else ei = new ExifInterface(selectedImage.getPath()); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: return rotateImage(img, 90); case ExifInterface.ORIENTATION_ROTATE_180: return rotateImage(img, 180); case ExifInterface.ORIENTATION_ROTATE_270: return rotateImage(img, 270); default: return img; } } 

我想这会解决你的问题