Android自定义录像(二)之录制文件存储与查看(附demo源码)
2016-08-10 21:15
597 查看
引言
这篇文章接着上一篇Android自定义录像(一)之录像功能实现(附demo源码)来写。这里将介绍用MediaRecorder录制视频后如何保存并且提供一个用户查看录像的Activity。用到的技术包括GridView的使用和文件存储与读取以及如何调用系统播放器播放等。后面附有github的demo源码下载链接。废话不多说,有代码有真相。思路与代码
首先我们写一个UI布局,这个布局很简单,是一个GridView,用于给用户展示录制好的录像列表。代码如下:<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <GridView android:gravity="center" android:id="@+id/gv_show_video" android:layout_width="fill_parent" android:layout_height="fill_parent" android:horizontalSpacing="2.0dip" android:verticalSpacing="2.0dip" android:stretchMode="columnWidth" android:columnWidth="100.0dip" android:numColumns="auto_fit" /> </LinearLayout>
GridView是Android很基础的控件,这里不多说。只是要注意一下这三个属性:
android:stretchMode="columnWidth" android:columnWidth="100.0dip" android:numColumns="auto_fit"
2.接着写java逻辑:
定义3个字段,分别是
private GridView gridView; private List<Map<String, Object>> items; private List<File> videoList ;
这里items是一个以map为存储结构的List,用于待会儿存储录像文件的icon和名称。
这里定义两个方法,一个是
GetFiles( )用来将文件从SD卡读取出来(前面第一篇博客中已经将录像的文件输出存储到SD卡新建的“VideoRecorderTest”目录中),另一个方法
FileToStr( List<File> f )则是用来把读取到的文件列表转换成字符串显示。
/** * 把文件列表转换成字符串 */ public String[] FileToStr( List<File> f ){ ArrayList<String> listStr = new ArrayList<String>(); int i; for (i = 0; i < f.size(); i++) { String nameString = f.get(i).getName(); listStr.add(nameString); } return listStr.toArray(new String[0]); } /** * 获取文件列表 */ public void GetFiles( ){ int i=0; File filePath=new File("sdcard/VideoRecorderTest"); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File[] files = filePath.listFiles(); for(i=0;i<files.length;i++){ if(files[i].getName().toLowerCase().endsWith(".mp4")) { videoList.add(files[i]); } } } items = new ArrayList<Map<String,Object>>(); for (int j = 0; j <i; j++) { Map<String, Object> item = new HashMap<String, Object>(); item.put("imageItem", R.drawable.folder);//添加图像资源的ID item.put("textItem",FileToStr(videoList)[j]); items.add(item); } }
在
GetFiles( )方法中有一段很关键的代码,用一个HashMap将文件列表存储起来并传到了刚才定义的items中。
items = new ArrayList<Map<String,Object>>(); for (int j = 0; j <i; j++) { Map<String, Object> item = new HashMap<String, Object>(); item.put("imageItem", R.drawable.folder);//添加图像资源的ID item.put("textItem",FileToStr(videoList)[j]); items.add(item); }
到这里items里面应存储了我们想展示
4000
给用户的东西。这时就到了把items的内容”装到“GridView中先是给用户界面了。GridView用adapter来赋予内容。这里定义一个
SimpleAdapter adapter = new SimpleAdapter(this,items,R.layout.item_show_video, new String[]{"imageItem", "textItem"},new int[]{R.id.image_item, R.id.text_item});
然后setadapter就哦了:
if(adapter!=null){ gridView.setAdapter(adapter);
最后用户看到的界面效果是这样的:
接下来写一下用户点击这些item弹出菜单选择系统播放器播放。
这个就很简单了:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Uri uri = Uri.parse("sdcard/VideoRecorderTest/"+FileToStr(videoList)[position]);//调用系统自带的播放器 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); startActivity(intent); } });
后言
到这里我们的功能就实现了。这里用到的几个小技术需要注意:GridView的使用和文件存储与读取以及如何调用系统播放器播放。小弟技术还很基础,如果有什么问题或者觉得代码写的不好的地方请各位不吝赐教。欢迎评论交流。
最后的最后
demo链接:
1.CSDN: http://download.csdn.net/detail/alanjet/95969152.GitHub: https://github.com/alanjet/VideoRecorderTest.git
相关文章推荐
- Android自定义录像(一)之录像功能实现(附demo源码)
- Android APK反编译查看源码及资源文件
- Android 自定义相机Demo源码
- Android开发进阶自定义控件之滑动开关实现方法【附demo源码下载】
- Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】
- Android:实际运用Zxing集成二维码扫描 及 自定义扫码界面(demo源码)
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
- Android APK反编译查看源码及资源文件
- 简单Demo让你分分钟理解Android中的文件存储
- 从头学android_通过查看Setting源码来获得剩余存储空间
- Android编程调用系统自带的拍照功能并返回JPG文件示例【附demo源码下载】
- Android APK反编译查看源码及资源文件
- 对Android APK文件反编译查看其源码
- 如何在网络上方便的查看并下载android源码文件
- 详解Android开发数据持久化之文件存储(附源码)
- Android APK反编译查看源码及资源文件
- android 录像,摄影自定义保存的文件名称
- 【2017 05 31】ADB的命令行使用——查看Android studio的Demo文件数据存储Sqlite
- Android APK反编译查看源码及资源文件
- Android App自定义签名文件的生成、查看SHA1值和使用