如何从完整图像中获取缩略图有几种解决方案,例如
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的图像
答案 0 :(得分:-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;
}
}
我认为这将解决您的问题