根据图片/视频路径获取缩略图路径

September 4, 2014

Android

不知道这个需求是不是很变态,但是确实遇到了这样的需求。大概的意思就是先从Images数据库里,根据路径查找到这个照片的ID,然后根据ID去Thumbnail库里找到缩略图的路径。视频也是类似。下面直接贴代码,没啥好说的。

package com.binkery.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;

public final class ThumbnailQuery {

    private static final String TAG = ThumbnailQuery.class.getSimpleName();

 public static String queryImageThumbnailByPath(Context context, String path) {
       Uri uri = Images.Media.EXTERNAL_CONTENT_URI;
     String[] projection = new String[] { Images.Media._ID };
     String selection = Images.Media.DATA + " = ? ";
      String[] selectionArgs = new String[] { path };

     Cursor cursor = query(context, uri, projection, selection,
               selectionArgs);
      int id = -1;
     if (cursor.moveToFirst()) {
          id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
     }
        cursor.close();
      if (id == -1) {
          return null;
     }

       uri = Images.Thumbnails.EXTERNAL_CONTENT_URI;
        projection = new String[] { Images.Thumbnails.DATA };
        selection = Images.Thumbnails.IMAGE_ID + " = ? ";
        selectionArgs = new String[] { String.valueOf(id) };

        cursor = query(context, uri, projection, selection, selectionArgs);
      String thumbnail = null;
     if (cursor.moveToFirst()) {
          int idxData = cursor.getColumnIndex(Images.Thumbnails.DATA);
         thumbnail = cursor.getString(idxData);
       }
        cursor.close();
      return thumbnail;
    }

   public static String queryVideoThumbnailByPath(Context context, String path) {
       Uri uri = Video.Media.EXTERNAL_CONTENT_URI;
      String[] projection = new String[] { Video.Media._ID };
      String selection = Video.Media.DATA + " = ? ";
       String[] selectionArgs = new String[] { path };

     Cursor cursor = query(context, uri, projection, selection,
               selectionArgs);
      int mediaId = -1;
        if (cursor.moveToFirst()) {
          int idxId = cursor.getColumnIndex(Video.Media._ID);
          mediaId = cursor.getInt(idxId);
      }
        cursor.close();
      if (mediaId == -1) {
         return null;
     }

       uri = Video.Thumbnails.EXTERNAL_CONTENT_URI;
     projection = new String[] { Video.Thumbnails.DATA };
     selection = Video.Thumbnails.VIDEO_ID + " =  ? ";
       selectionArgs = new String[] { String.valueOf(mediaId) };

       cursor = query(context, uri, projection, selection, selectionArgs);
      String thumbnail = null;
     if (cursor.moveToFirst()) {
          int idxData = cursor.getColumnIndex(Video.Thumbnails.DATA);
          thumbnail = cursor.getString(idxData);
       }
        cursor.close();
      return thumbnail;
    }

   private static Cursor query(Context context, Uri uri, String[] projection,
           String selection, String[] selectionArgs) {
      ContentResolver cr = context.getContentResolver();
       Cursor cursor = cr.query(uri, projection, selection, selectionArgs,
              null);
       return cursor;
   }

}

--- EOF ---

评论

  1. qyvlik qyvlik

    由于我需要在 C++ 中调用这个 Java 代码。所以 queryImageThumbnailByPath 这个函数传入的参数可以是 应用的主 Activity 吗?

    1. 当前 Activity 或者 Context 都可以。

  2. xy1365 xy1365

    楼主你好,想请教一下,我在获得图片ID那里都是可以的,但是根据ID找缩略图却找不到,是什么原因啊?这个方法正好是我想实现的效果 还望不吝赐教!非常感谢!!!!

  3. 我不清楚为啥会有些图片没有生成缩略图,可能是系统没有扫描到,如果是这种的话,你可以通知系统扫描一下你需要的图片所在的目录,系统扫描的时候会抽出缩略图的。或者可能是系统抽缩略图的时候出现了错误,没有正常的生成缩略图。 如果确实是没有缩略图,只能自己 decode 了,用 BitmapFactory decode 方法。

添加新评论