从图片网址获取缩略图网址 [英] Get thumbnail URL from image URL
问题描述
我有一个String URL指向存储在设备外部存储中的图像:
I have a String URL pointing to an image stored on the external storage of my device:
String imageUrl = "/storage/emulated/0/DCIM/100MEDIA/IMAG0823.jpg"
我想查询 MediaStore
以获取此图像的缩略图.这就是我现在正在做的事情:
I want to get query the MediaStore
to get the thumbnail for this image. This is what I do right now:
private String getImageThumbnailPath(Context ctx, String imageUrl){
Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
ctx.getContentResolver(), Uri.fromFile(new File(imageUrl)),
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
String url = "";
if( cursor != null && cursor.getCount() > 0 ) {
cursor.moveToFirst();
url = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
cursor.close();
}
return url;
}
但是,调用此方法并打印其内容不会显示任何内容(光标为空).
However, calling this method and printing it's content shows nothing (the cursor is empty).
如何在 MediaStore
中查询与我的图片网址相关联的缩略图网址?
How do I query the MediaStore
for the thumbnail url associated with my image URL?
修改
我还尝试过直接从图片URL解析Uri,就像这样:
I've also tried to parse the Uri directly from the image URL, as so:
Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
ctx.getContentResolver(), Uri.parse(imageUrl),
MediaStore.Images.Thumbnails.MINI_KIND,
null);
但是结果是一样的.
推荐答案
如果您从图库中选择图像,以下代码将起作用,否则我们将无法创建缩略图,而必须创建缩略图.
Following code works if you have pick image from gallery, otherwise we can not have thumbnail, and we have to create thumbnail.
首先,您必须找到 MediaStore.Images.Media._ID
public String[] getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor cursor = getActivity().getContentResolver().query(contentUri,
proj, null, null, null);
int path_index = cursor.getColumnIndexOrThrow(proj[0]);
int id_index = cursor.getColumnIndexOrThrow(proj[1]);
cursor.moveToFirst();
return new String[] { cursor.getString(path_index),
cursor.getLong(id_index) + "" };
}
从上面的 getRealPathFromURI
上面,我们有了 MediaStore.Images.Media._ID
,请使用此ID查找缩略图.
From above getRealPathFromURI
now we have MediaStore.Images.Media._ID
, use this id to find thumbnail.
public static Bitmap getThumbnail(ContentResolver contentResolver, long id) {
Cursor cursor = contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media.DATA}, // Which columns
// to return
MediaStore.Images.Media._ID + "=?", // Which rows to return
new String[]{String.valueOf(id)}, // Selection arguments
null);// order
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String filePath = cursor.getString(0);
cursor.close();
int rotation = 0;
try {
ExifInterface exifInterface = new ExifInterface(filePath);
int exifRotation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
if (exifRotation != ExifInterface.ORIENTATION_UNDEFINED) {
switch (exifRotation) {
case ExifInterface.ORIENTATION_ROTATE_180:
rotation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotation = 90;
break;
}
}
} catch (IOException e) {
Log.e("getThumbnail", e.toString());
}
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
contentResolver, id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(rotation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
return bitmap;
} else
return null;
}
要在 LOC
已更新
String[] imageInfo = getRealPathFromURI(Uri.parse("YOUR_IMAGE_PATH"));
yourImageView.setImageBitmap(getThumbnail(getActivity()
.getContentResolver(), Long.parseLong(imageInfo[1])));
Uri.parse("YOUR_IMAGE_PATH")
是contentUri
Uri.parse("YOUR_IMAGE_PATH")
is contentUri
这篇关于从图片网址获取缩略图网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!