實作功能:
- 可檢視普通錄像、緊急錄像和照片
- 分前置攝像頭和後置攝像頭檢視
- 長按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();
}
}