Android系統啟動時會去掃描系統文件,並將系統支持的視頻文件(mp4,3gp,wmv)掃描到媒體庫(MediaStore)中,下面代碼演示如何獲得這些文件的信息:
- 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());
- }
- }
有一點需要注意的是:系統的媒體庫並不會在我們添加視頻文件後自動更新,我們如何去手動掃描媒體庫,或者重啟系統才能從媒體庫中得到更新的視頻文件:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));