android - 从Android上的缩略图查询中获取图像

标签 android image-processing

我有这段代码(在这个网站的某处找到):

    public static List<MyImages> getImages(Activity context) {
    List<MyImages> lst = new ArrayList<MyImages>();
    Cursor cursor = getCameraThumbImages(context);
    if (cursor != null) {
        int columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int columnIndexPath = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
        int columnIndexImagePath = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID);
        int count = cursor.getCount();
        for (int i = 0; i < count; i++) {
            cursor.moveToPosition(i);

            int imageID = cursor.getInt(columnIndex);
            String path = cursor.getString(columnIndexPath);
            Uri imgThmbPath = Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
                            + imageID);
            String hope = cursor.getString(columnIndexImagePath);
            MyImages p2p = new MyImages(path, "" + imageID);
            lst.add(p2p);
        }
    }

    return lst;
}

此代码允许我在手机上访问图像的缩略图。问题是我看不到如何从中获取原始图像路径。

问题是:给定缩略图(或光标),如何获取原始图像路径?

最佳答案

缩略图中有 MediaStore.Images.Thumbnails.IMAGE_ID 字段,您可以从中获取相关的图像 ID。比查询 MediaStore.Images.Media 并从 MediaStore.Images.Media.DATA 字段获取您的照片的路径。

编辑

// First request thumbnails what you want
String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
Cursor thumbnails = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);

// Then walk thru result and obtain imageId from records
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
    String imageId = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.IMAGE_ID));

    // Request image related to this thumbnail 
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor images = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {imageId}, null);

    if (cursor != null && cursor.moveToFirst()) {
        // Your file-path will be here
        String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
    }

}

//Of course you need to restrict queries using selection and selection args params and get only rows that you really need

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

相关文章:

java - 在 PNG 图像上获取适合的矩形

java - 我的 soundpool 项目无法正常播放?

Android volley 错误 : "Trust anchor for certification path not found", 仅在真实设备中,而不是模拟器

安卓布局 : Game Board

Android:Parcelable 和 Serializable 之间的区别?

python - 如何裁剪图像,如果坐标不存在,则延伸另一侧,从而保持裁剪的大小相同?

image - 如何为 10 亿张 png 图像生成统一缩略图?

java - Android:将 Drawable 添加到现有的 LayerDrawable

opencv - 从图像中提取线条以提供给 OCR - Tesseract

python - python-pillow 中内核大小大于 5x5 的卷积