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

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 都可以。

添加新评论