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的图像

One Solution collect form web for “MediaStore.Images从拇指Uri / id获取完整图像”

在获得图像的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; } } 

我想这会解决你的问题

  • 如何映射java.util.Optional <Something>到某些东西? 在Kotlin
  • 如何访问Kotlin中的“Activity.this”?
  • 如何在初始化Kotlin对象时存储临时变量?
  • 如何在Android Activity上使用Kotlin扩展函数进行空检查
  • 在Android Studio中将“java”目录重命名为“kotlin”
  • 我如何从定义类的外部调用扩展方法?
  • NoSuchMethodError使用Kotlin与新的Android数据绑定
  • 用于构建gradle(1.1.2-5)的kotlin版本与捆绑到IDE插件(1.1.2-4)中的版本不同,
  • Anko对话框按钮标题
  • 从Kotlin Anko的两个表中选择数据
  • 导入和锯齿实践
  • Kotlin language will be the best programming language for Android.