如何扫描出android系统媒体库中视频文件

转于:http://my.oschina.net/AlexZhuang/blog/57695

------------------------------------------------------------------------

 

android系统启动时会去扫描系统文件,并将系统支持的视频文件(mp4,3gp,wmv)扫描到媒体库(MediaStore)中,下面代码演示如何获得这些文件的信息:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static List<VideoInfo> sysVideoList = null ; // 视频信息集合
sysVideoList = new ArrayList<VideoInfo>();
setVideoList();
 
     private void setVideoList() {
         // MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径
         String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
                 MediaStore.Video.Thumbnails.VIDEO_ID };
 
         // MediaStore.Video.Media.DATA:视频文件路径;
         // MediaStore.Video.Media.DISPLAY_NAME : 视频文件名,如 testVideo.mp4
         // MediaStore.Video.Media.TITLE: 视频标题 : testVideo
         String[] mediaColumns = { MediaStore.Video.Media._ID,
                 MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
                 MediaStore.Video.Media.MIME_TYPE,
                 MediaStore.Video.Media.DISPLAY_NAME };
 
         cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                 mediaColumns, null , null , null );
         
         if (cursor== null ){
             Toast.makeText(SystemVideoChooseActivity. this , "没有找到可播放视频文件" , 1 ).show();
             return ;
         }
         if (cursor.moveToFirst()) {
             do {
                 VideoInfo info = new VideoInfo();
                 int id = cursor.getInt(cursor
                         .getColumnIndex(MediaStore.Video.Media._ID));
                 Cursor thumbCursor = managedQuery(
                         MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                         thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
                                 + "=" + id, null , null );
                 if (thumbCursor.moveToFirst()) {
                     info.setThumbPath(thumbCursor.getString(thumbCursor
                             .getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
                 }
                 info.setPath(cursor.getString(cursor
                         .getColumnIndexOrThrow(MediaStore.Video.Media.DATA)));
                 info.setTitle(cursor.getString(cursor
                         .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)));
                 
                 info.setDisplayName(cursor.getString(cursor
                         .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)));
                 LogUtil.log(TAG, "DisplayName:" +info.getDisplayName());
                 info.setMimeType(cursor
                         .getString(cursor
                                 .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
                 
                 sysVideoList.add(info);
             } while (cursor.moveToNext());
         }
     }

 

有一点需要注意的是:系统的媒体库并不会在我们添加视频文件后自动更新,我们如何去手动扫描媒体库,或者重启系统才能从媒体库中得到更新的视频文件:

 

 

?
1
2
sendBroadcast( new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse( "file://" 
                 + Environment.getExternalStorageDirectory())));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值