android 获取视频,图片缩略图的具体实现
1、获取视频缩略图有两个方法(1)通过内容提供器来获取(2)人为创建缩略图
(1)缺点就是必须更新媒体库才能看到最新的视频的缩略图
[java]
/**
* @param context
* @param cr
* @param videopath
* @return
*/
public static bitmap getvideothumbnail(context context, contentresolver cr, string videopath) {
contentresolver testcr = context.getcontentresolver();
string[] projection = { mediastore.video.media.data, mediastore.video.media._id, };
string whereclause = mediastore.video.media.data + " = '" + videopath + "'";
cursor cursor = testcr.query(mediastore.video.media.external_content_uri, projection, whereclause,
null, null);
int _id = 0;
string videopath = "";
if (cursor == null || cursor.getcount() == 0) {
return null;
}
if (cursor.movetofirst()) {
int _idcolumn = cursor.getcolumnindex(mediastore.video.media._id);
int _datacolumn = cursor.getcolumnindex(mediastore.video.media.data);
do {
_id = cursor.getint(_idcolumn);
videopath = cursor.getstring(_datacolumn);
} while (cursor.movetonext());
}
cursor.close();
bitmapfactory.options options = new bitmapfactory.options();
options.indither = false;
options.inpreferredconfig = bitmap.config.rgb_565;
bitmap bitmap = mediastore.video.thumbnails.getthumbnail(cr, _id, images.thumbnails.mini_kind,
options);
return bitmap;
}
/**
* @param context
* @param cr
* @param videopath
* @return
*/
public static bitmap getvideothumbnail(context context, contentresolver cr, string videopath) {
contentresolver testcr = context.getcontentresolver();
string[] projection = { mediastore.video.media.data, mediastore.video.media._id, };
string whereclause = mediastore.video.media.data + " = '" + videopath + "'";
cursor cursor = testcr.query(mediastore.video.media.external_content_uri, projection, whereclause,
null, null);
int _id = 0;
string videopath = "";
if (cursor == null || cursor.getcount() == 0) {
return null;
}
if (cursor.movetofirst()) {
int _idcolumn = cursor.getcolumnindex(mediastore.video.media._id);
int _datacolumn = cursor.getcolumnindex(mediastore.video.media.data);
do {
_id = cursor.getint(_idcolumn);
videopath = cursor.getstring(_datacolumn);
} while (cursor.movetonext());
}
cursor.close();
bitmapfactory.options options = new bitmapfactory.options();
options.indither = false;
options.inpreferredconfig = bitmap.config.rgb_565;
bitmap bitmap = mediastore.video.thumbnails.getthumbnail(cr, _id, images.thumbnails.mini_kind,
options);
return bitmap;
}(2)人为创建缩略图要耗费一点时间
[java]
/**
* 获取视频缩略图
* @param videopath
* @param width
* @param height
* @param kind
* @return
*/
private bitmap getvideothumbnail(string videopath, int width , int height, int kind){
bitmap bitmap = null;
bitmap = thumbnailutils.createvideothumbnail(videopath, kind);
bitmap = thumbnailutils.extractthumbnail(bitmap, width, height, thumbnailutils.options_recycle_input);
return bitmap;
}
/**
* 获取视频缩略图
* @param videopath
* @param width
* @param height
* @param kind
* @return
*/
private bitmap getvideothumbnail(string videopath, int width , int height, int kind){
bitmap bitmap = null;
bitmap = thumbnailutils.createvideothumbnail(videopath, kind);
bitmap = thumbnailutils.extractthumbnail(bitmap, width, height, thumbnailutils.options_recycle_input);
return bitmap;
}
2、图片缩略图
[java]
/**
*
* @param context
* @param cr
* @param imagepath
* @return
*/
public static bitmap getimagethumbnail(context context, contentresolver cr, string imagepath) {
contentresolver testcr = context.getcontentresolver();
string[] projection = { mediastore.images.media.data, mediastore.images.media._id, };
string whereclause = mediastore.images.media.data + " = '" + imagepath + "'";
cursor cursor = testcr.query(mediastore.images.media.external_content_uri, projection, whereclause,
null, null);
int _id = 0;
string imagepath = "";
if (cursor == null || cursor.getcount() == 0) {
return null;
}
if (cursor.movetofirst()) {
int _idcolumn = cursor.getcolumnindex(mediastore.images.media._id);
int _datacolumn = cursor.getcolumnindex(mediastore.images.media.data);
do {
_id = cursor.getint(_idcolumn);
imagepath = cursor.getstring(_datacolumn);
} while (cursor.movetonext());
}
cursor.close();
bitmapfactory.options options = new bitmapfactory.options();
options.indither = false;
options.inpreferredconfig = bitmap.config.rgb_565;
bitmap bitmap = mediastore.images.thumbnails.getthumbnail(cr, _id, images.thumbnails.mini_kind,
options);
return bitmap;
}
/**
*
* @param context
* @param cr
* @param imagepath
* @return
*/
public static bitmap getimagethumbnail(context context, contentresolver cr, string imagepath) {
contentresolver testcr = context.getcontentresolver();
string[] projection = { mediastore.images.media.data, mediastore.images.media._id, };
string whereclause = mediastore.images.media.data + " = '" + imagepath + "'";
cursor cursor = testcr.query(mediastore.images.media.external_content_uri, projection, whereclause,
null, null);
int _id = 0;
string imagepath = "";
if (cursor == null || cursor.getcount() == 0) {
return null;
}
if (cursor.movetofirst()) {
int _idcolumn = cursor.getcolumnindex(mediastore.images.media._id);
int _datacolumn = cursor.getcolumnindex(mediastore.images.media.data);
do {
_id = cursor.getint(_idcolumn);
imagepath = cursor.getstring(_datacolumn);
} while (cursor.movetonext());
}
cursor.close();
bitmapfactory.options options = new bitmapfactory.options();
options.indither = false;
options.inpreferredconfig = bitmap.config.rgb_565;
bitmap bitmap = mediastore.images.thumbnails.getthumbnail(cr, _id, images.thumbnails.mini_kind,
options);
return bitmap;
}