一、准备工作
插入相机权限和文件读取权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
由于6.0以上使用动态权限获取,这里使用rxpermissions动态获取
在Module中的build.gradle中国添加依赖
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
compile 'io.reactivex:rxjava:1.1.3'
二、开始写代码
1.定义布局界面
这里定义两个按钮,一个是拍照,一个是拍视频,下面的ImageView是存放缩略图的,这里只是实例,如有需要可以改成网格列表存放管理。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xuan.camera.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍视频"
android:id="@+id/open_camera"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照片"
android:id="@+id/open_photo"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/video_thumbnail"/>
</LinearLayout>
2.逻辑代码
1>初始化变量
private Boolean mPermission = false;
private final int REQUEST_CODE_CAMERA = 1;
private final int REQUEST_CODE_PHOTO = 101;
private Button open_camera;
private Button open_photo;
private String videoId;
private ImageView video_thumbnail;
2>在setContentView之上添加动态权限获取的代码
这里如果获取成功给mPermission赋值true,否则为false
RxPermissions.getInstance(this)
.request(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean aBoolean) {
if (aBoolean){
mPermission = true;
}
}
});
setContentView(R.layout.activity_main);
3>对按钮和图片控件进行初始化
open_camera = (Button) findViewById(R.id.open_camera);
open_photo = (Button) findViewById(R.id.open_photo);
video_thumbnail = (ImageView) findViewById(R.id.video_thumbnail);
4>判断是否有权限打开相机和存储
有权限的话调用OnClick事件打开相应的方法
if (mPermission){
open_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openCamera();
}
});
open_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openPhoto();
}
});
}else{
Toast.makeText(this, "请先获取权限", Toast.LENGTH_SHORT).show();
}
5>打开拍照和录像的方法
public void openCamera(){
//初始化file路径
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"myVideo");
//如果没有这个路径存在,生成一个
if (!file.exists()){
file.mkdir();
}
//初始化录像的名称,为年月日的格式
String videoName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".mp4";
//将录像的名称存储起来,方便后期生成缩略图
videoId = videoName;
//新建录像文件
File saveFile = new File(file,videoName);
//获取saveFile的uri地址
Uri uri = Uri.fromFile(saveFile);
//通过Intent打开内部MediaStore的相机界面
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
//限时录制30秒
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,30);
//设置输出的uri地址,存储到相应的位置
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
//会有回调函数,方便回调
startActivityForResult(intent,REQUEST_CODE_CAMERA);
}
//内部方法与录像的相同,这里不再赘述
public void openPhoto(){
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"myPhoto");
if (!file.exists()){
file.mkdirs();
}
String photoName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".jpg";
File saveFile = new File(file,photoName);
Uri uri = Uri.fromFile(saveFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,REQUEST_CODE_PHOTO);
}
6>重写回调函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK){
if (requestCode == REQUEST_CODE_CAMERA){
Toast.makeText(this, "保存视频成功", Toast.LENGTH_SHORT).show();
//调用getThumbnail方法生成一个200*200的缩略图,并存储到Bitmap中
Bitmap map = getThumbnail(Environment.getExternalStorageDirectory()+File.separator+"myVideo"+File.separator+videoId,200,200, MediaStore.Images.Thumbnails.MICRO_KIND);
//设置缩略图
video_thumbnail.setImageBitmap(map);
}
if (requestCode == REQUEST_CODE_PHOTO){
Toast.makeText(this, "保存照片成功", Toast.LENGTH_SHORT).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
7>获取缩略图的方法
通过ThumbnailUtils工具获取视频的缩略图,具体方法是从是视频中截取一帧作为图片
public static Bitmap getThumbnail(String videoPath,int Width,int Height,int Kind){
Bitmap bitmap = null;
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath,Kind);
bitmap = ThumbnailUtils.extractThumbnail(bitmap,Width,Height,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}