1.布局一个ImageView和一个Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.yaya.recordvideo.MainActivity">
<ImageView
android:id="@+id/iv_record_video"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_record"
android:text="@string/record_video"/>
</LinearLayout>
2.添加相关权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.MainActivity中代码的实现
package com.yaya.recordvideo;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private Button btn_record;
private final int RESULT_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv_record_video);
btn_record = (Button) findViewById(R.id.btn_record);
btn_record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//限制时长s
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
//限制大小
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 10*1024*1024);
//设置质量
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//设置输出位置
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(Environment.getExternalStorageDirectory()+ File.separator+"gridview"+File.separator+System.currentTimeMillis()+".mp4"));
startActivityForResult(intent, 1);
}
});
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = (String) iv.getTag();
if(path != null){
Intent videoFileIntent = getVideoFileIntent(new File(path));
startActivity(videoFileIntent);
}else{
Toast.makeText(MainActivity.this, "不能打开这个视频", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 利用android系统播放添加好的视频
* @param videoFile
* @return
*/
public Intent getVideoFileIntent(File videoFile)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(videoFile);
intent.setDataAndType(uri, "video/*");
return intent;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Uri uri = data.getData();
//视频地址
String videoPath = getPathFromUri(this,uri);
//系统工具类ThumbnailUtils 获取视频的第一帧
Bitmap videoThumbnail = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Video.Thumbnails.MICRO_KIND);
if(videoThumbnail != null){
iv.setImageBitmap(videoThumbnail);
iv.setTag(videoPath);
}else{
Toast.makeText(this, "获取缩略图失败拉!", Toast.LENGTH_SHORT).show();
}
}
}
public static String getPathFromUri(Context mContext,Uri contentUri){
String[] proj = { MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String string = cursor.getString(column_index);
cursor.close();
return string;
}
}
网友评论