Android-android 4.0怎么获取视频的缩略图?
在2.X的系统上我能获取到视频缩略图,但是到了4.0却获取不到;是不是4.0获取的路径改了?
我以前获取的方法如下:
String[] fileds = {
MediaStore.Video.Media.DATA,
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME
};
ContentResolver provider = m_context.getContentResolver();
Cursor cursor = MediaStore.Video.query(provider, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, fileds);
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
String url = cursor.getString(0);//.substring(4);
File file = new File(url);
if((url.endsWith("mp4") || url.endsWith("3gp") || url.endsWith("flv")) && file.exists())
{
String img =String.valueOf(cursor.getLong(1));
}
cursor.moveToNext();
}
Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentProvider, Long.valueOf(img), MediaStore.Video.Thumbnails.MICRO_KIND, null);
这个应用是基于2.1系统开发的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哈哈,这个我前几天刚刚弄过,代码,你应该懂得
private String[] mediaColumns = new String[] {
MediaStore.Video.Media.DATA, MediaStore.Video.Media.MIME_TYPE,
};
Cursor cursor = cursor = managedQuery(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns,
null, null, null);
if (cursor.moveToFirst()) {
do {
VideoInfo info = new VideoInfo();
info.filePath = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
info.mimeType = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
info.filePath, Thumbnails.MICRO_KIND);
int width = MainActivity.SCREEN_WIDTH / 4;
int height = width * 3 / 4;
Bitmap bitmap1 = ThumbnailUtils.extractThumbnail(bitmap,
width, height);
info.bitmap = bitmap1;
bitmap.recycle();
videoList.add(info);
} while (cursor.moveToNext());
}
cursor.close();
info.bitmap,就是用来存储视频截图的。
核心就是,利用ThumbnailUtils类,这个方法createVideoThumbnail只要接受路径 and 数据类型,就可以返回视频截图了。
static public Bitmap getVideoThumbnail(ContentResolver cr, String path) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//select condition.
String whereClause = MediaStore.Video.Media.DATA + " = '"
+ path + "'";
Log.v(TAG, "where = " + whereClause);
//colection of results.
Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Video.Media._ID }, whereClause,
null, null);
Log.v(TAG, "cursor = " + cursor);
if (cursor == null || cursor.getCount() == 0) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
}
cursor.moveToFirst();
//image id in image table.
String videoId = cursor.getString(cursor
.getColumnIndex(MediaStore.Video.Media._ID));
Log.v(TAG, "videoId = " + videoId);
if (videoId == null) {
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
}
cursor.close();
long videoIdLong = Long.parseLong(videoId);
//via imageid get the bimap type thumbnail in thumbnail table.
bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,
Images.Thumbnails.MICRO_KIND, options);
Log.v(TAG, "bitmap = " + bitmap);
return bitmap;
}
你试试,我的4.0.3上面试过可以的。