【问题标题】:MediaStore.Images get full image from thumb Uri /idMediaStore.Images 从拇指 Uri /id 获取完整图像
【发布时间】:2017-09-24 17:18:58
【问题描述】:

关于如何从完整图像中获取缩略图有两种解决方案,例如
android get thumbnail of image stored on sdcard whose path is known

但是,相反,我需要从缩略图 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:未知 URL:content://media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg 在 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)

Picasso 也无法从此 Uri 加载图像

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    获取图片的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;
        }
    }
    

    我想这会解决你的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多