android - 从图像 URL 获取缩略图 URL

标签 android

我有一个字符串 URL 指向存储在我设备外部存储器上的图像:

String imageUrl = "/storage/emulated/0/DCIM/100MEDIA/IMAG0823.jpg"

我想查询 MediaStore 以获取此图像的缩略图。这就是我现在所做的:

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;
} 

但是,调用此方法并打印其内容时什么也没有显示(光标为空)。

如何在 MediaStore 中查询与我的图像 URL 关联的缩略图 url?

编辑

我也尝试过直接从图片 URL 解析 Uri,如下所示:

Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnails(
        ctx.getContentResolver(), Uri.parse(imageUrl),
        MediaStore.Images.Thumbnails.MINI_KIND,
        null);

但结果是一样的。

最佳答案

如果您从图库中选择图像,则以下代码有效,否则我们不能有缩略图,我们必须创建缩略图。

首先,您必须找到 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 来查找缩略图。

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

关于android - 从图像 URL 获取缩略图 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029011/

相关文章:

java - Appium : how to check device name using automation test cases in java

android - 在android studio中构建失败

android - ListView 不可点击

java - 你怎么能比较android中的字符串大于

java - 尝试从空对象引用上的字段 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' 读取

android - 仅在安装时在 phonegap android 应用程序中创建数据库

android - 如何在 Kotlin 多平台项目的 iOS 模块中将字符串转换为字节数组?

android - 帮助我为 2D 配置 OpenGL

java - Android 使用 opencv 对静态图像进行人脸检测?

android - 如何与适配器类共享数组列表