Java代码
1 privateBitmapcreateVideoThumbnail(StringfilePath){
2 Bitmapbitmap=null;
3 MediaMetadataRetrieverretriever=newMediaMetadataRetriever();
4 try{
5 retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
6 retriever.setDataSource(filePath);
7 bitmap=retriever.captureFrame();
8 }catch(IllegalArgumentExceptionex){
9 //Assumethisisacorruptvideofile
10 }catch(RuntimeExceptionex){
11 //Assumethisisacorruptvideofile.
12 }finally{
13 try{
14 retriever.release();
15 }catch(RuntimeExceptionex){
16 //Ignorefailureswhilecleaningup.
17 }
18 }
19 returnbitmap;
20 }
21
22 privatevoidgetVideoFile(finalList<videoItem>list)
23 {
24 Bitmapbitmap=null;
25
26 ContentResolvermContentResolver=this.getContentResolver();
27 Cursorcursor=mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,null,
28 null,null,MediaStore.Video.DEFAULT_SORT_ORDER);
29
30
31
32 if(cursor.moveToFirst())
33 {
34 do{
35 //ID:MediaStore.Audio.Media._ID
36 intid=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
37
38 //名称:MediaStore.Audio.Media.TITLE
39 Stringtilte=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
40
41 //专辑名:MediaStore.Audio.Media.ALBUM
42 Stringalbum=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
43
44
45 //歌手名:MediaStore.Audio.Media.ARTIST
46 Stringartist=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
47
48 //路径:MediaStore.Audio.Media.DATA
49 Stringurl=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
50
51 //总播放时长:MediaStore.Audio.Media.DURATION
52 intduration=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
53
54 //大小:MediaStore.Audio.Media.SIZE
55 intsize=(int)cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
56
57 //拍摄时间
58 intdateTaken=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN));
59 Stringdatetime=DateFormat.format("yyyy-MM-ddkk:mm:ss",dateTaken).toString();
60
61 bitmap=createVideoThumbnail(url);
62
63 }
MediaMetadataRetriever.Java 应放在工程的: Android.media 下面