实现功能:
- 可查看普通录像、紧急录像和照片
- 分前置摄像头和后置摄像头查看
- 长按item进入多选模式,可以加锁解锁或删除
- 上下一部视频切换功能
- 进入应用暂停录像,退出后自动开启
使用嵌套Fragment实现框架:
videoUnlockMainFragment = new UnlockMainFragment();
videoLockMainFragment = new LockMainFragment();
photoMainFragment = new PhotoMainFragment();
fragmentArray = new Fragment[] { videoUnlockMainFragment,
videoLockMainFragment, photoMainFragment };
...
...
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, videoUnlockMainFragment)
.add(R.id.fragmentContainer, videoLockMainFragment)
.add(R.id.fragmentContainer, photoMainFragment)
.hide(videoLockMainFragment).hide(photoMainFragment)
.show(videoUnlockMainFragment).commit();

长按进入多选模式:
AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
viewGroupTitleBar.setVisibility(View.VISIBLE);
viewParams.setShownStyle(ShownStyle.PICK_MULTIPLE);
multiSelectGridView.setFastScrollEnabled(false);
multiSelectGridAdapter.notifyDataSetChanged();
return true;
}
};
上下一部和播放暂停的控制:
case R.id.btnNext:
if (videoPosition + < MyApp.arrayListVideo.size()) {
playVideoByPosition(videoPosition + );
cancelDelayHide();
hideControllerDelay();
} else {
HintUtil.showToast(context,
getString(R.string.video_play_alredy_last));
}
break;
case R.id.btnPrevious:
if (videoPosition - >=
&& videoPosition - < MyApp.arrayListVideo.size()) {
playVideoByPosition(videoPosition - );
cancelDelayHide();
hideControllerDelay();
} else {
HintUtil.showToast(context,
getString(R.string.video_play_alredy_first));
}
break;
case R.id.btnPlayState:
cancelDelayHide();
if (isPaused) {
videoView.start();
btnPlayState.setImageResource(R.drawable.btn_video_pause);
hideControllerDelay();
} else {
videoView.pause();
btnPlayState.setImageResource(R.drawable.btn_video_play);
}
isPaused = !isPaused;
break;
获取指定目录的视频文件:
public static ArrayList<MultiGridItem> getVideos(ContentResolver cr,
boolean isFront, boolean isLock) {
ArrayList<MultiGridItem> arrayListVideo = new ArrayList<MultiGridItem>();
File dirMedia;
if (!isFront) { // 后录视频
dirMedia = new File(isLock ? Constant.Path.VIDEO_BACK_LOCK
: Constant.Path.VIDEO_BACK_UNLOCK);
} else { // 前录视频
dirMedia = new File(isLock ? Constant.Path.VIDEO_FRONT_LOCK
: Constant.Path.VIDEO_FRONT_UNLOCK);
}
try {
if (dirMedia.exists()) {
File[] childFiles = dirMedia.listFiles();
for (File childFile : childFiles) {
String fileName = childFile.getName();
if (!fileName.startsWith(".") && fileName.endsWith(".mp4")
&& childFile.exists()) {
MultiGridItem item = new MultiGridItem();
item.path = childFile.getPath();
item.isVideo = true;
item.displayName = childFile.getName();
String thumbnailPath = Constant.Path.VIDEO_THUMBNAIL
+ fileName.replace(".mp4", ".jpg");
if (new File(thumbnailPath).exists()) {
item.thumbnail = "file://" + thumbnailPath;
} else {
item.bitmapThumbnail = getVideoThumbnail(
childFile.getPath(), , ,
MediaStore.Video.Thumbnails.MICRO_KIND);
}
arrayListVideo.add(item);
}
}
} else {
MyLog.i("getVideos.Directory not exist");
}
} catch (Exception e) {
e.printStackTrace();
MyLog.e("getVideos catch Exception:" + e.toString());
}
Collections.reverse(arrayListVideo);
return arrayListVideo;
}
获取照片:
public static ArrayList<MultiGridItem> getPhotos(ContentResolver resolver,
boolean isFront) {
ArrayList<MultiGridItem> galleryList = new ArrayList<MultiGridItem>();
try {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = resolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
if (imagecursor != null && imagecursor.getCount() > ) {
while (imagecursor.moveToNext()) {
MultiGridItem item = new MultiGridItem();
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
String imagePath = imagecursor.getString(dataColumnIndex);
item.path = "file://" + imagePath;
// DATA:/storage/sdcard1/DrivingRecord/Image/--_150926_1.jpg
if (imagePath
.startsWith("/storage/sdcard1/DrivingRecord/Image/20")) {
item.displayName = imagePath.split("/")[];
}
if (item.path.endsWith(isFront ? "_0.jpg" : "_1.jpg")) {
galleryList.add(item);
}
}
}
} catch (Exception e) {
e.printStackTrace();
MyLog.e("getPhotos catch Exception:" + e.toString());
}
// show newest photo at beginning of the list
Collections.reverse(galleryList);
return galleryList;
}
删除对话框:
private void showDeleteProgressDialog(String[] paths) {
final int itemSize = paths.length;
final String[] itemPaths = paths;
if (itemSize > ) {
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(getString(R.string.dialog_delete_title));
dialog.setMessage(getString(R.string.dialog_delete_wait));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(itemSize);
dialog.setCancelable(false);
dialog.show();
dialog.setProgress();
new Thread() {
public void run() {
for (int i = ; i < itemSize; i++) {
String path = itemPaths[i];
if (path != null) {
File fileDelete = new File(path);
if (fileDelete.exists() && fileDelete.isFile()) {
fileDelete.delete();
}
dialog.setProgress(i + );
}
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
HintUtil.showToast(context, context.getResources()
.getString(R.string.dialog_delete_success));
dialog.dismiss();
reloadContent();
}
});
}
}.start();
}
}
加锁对话框:
private void showLockProgressDialog(String[] paths) {
final int itemSize = paths.length;
final String[] itemPaths = paths;
if (itemSize > ) {
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(getString(R.string.dialog_lock_title));
dialog.setMessage(getString(R.string.dialog_lock_wait));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(itemSize);
dialog.setCancelable(false);
dialog.show();
dialog.setProgress();
new Thread() {
public void run() {
for (int i = ; i < itemSize; i++) {
String path = itemPaths[i];
if (path != null
&& path.startsWith(Constant.Path.VIDEO_FRONT_UNLOCK)) {
String videoName = path.split("/")[];
StorageUtil.lockVideo(true, videoName);
dialog.setProgress(i + );
}
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
HintUtil.showToast(context,
getString(R.string.dialog_lock_success));
dialog.dismiss();
reloadContent();
}
});
}
}.start();
}
}
解锁对话框:
private void showUnlockProgressDialog(String[] paths) {
final int itemSize = paths.length;
final String[] itemPaths = paths;
if (itemSize > ) {
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(getString(R.string.dialog_unlock_title));
dialog.setMessage(getString(R.string.dialog_unlock_wait));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(itemSize);
dialog.setCancelable(false);
dialog.show();
dialog.setProgress();
new Thread() {
public void run() {
for (int i = ; i < itemSize; i++) {
String path = itemPaths[i];
if (path != null
&& path.startsWith(Constant.Path.VIDEO_FRONT_LOCK)) {
String videoName = path.split("/")[];
StorageUtil.unlockVideo(true, videoName);
dialog.setProgress(i + );
}
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
HintUtil.showToast(context,
getString(R.string.dialog_unlock_success));
dialog.dismiss();
reloadContent();
}
});
}
}.start();
}
}