增加权限
<uses-feature android:name="android.hardware.camera" android:required="false" />
JAVA代码
package video;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
import com.example.myapp_b.R;
public class VideoActivity extends AppCompatActivity {
VideoView vv;
static final int REQUEST_VIDEO_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
vv=findViewById(R.id.vv);
}
public void takeVideo(View view) {
Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//1.获取数据uri
super.onActivityResult(requestCode, resultCode, intent);
Uri videoUri = intent.getData();
//2.实例化媒体控制器
MediaController controller = new MediaController(this);
//3.设置播放源
vv.setVideoURI(videoUri);
//4.媒体控制器与视频控件相互关联
controller.setMediaPlayer(vv);
vv.setMediaController(controller);
}
}
xml
<?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"
android:orientation="vertical"
tools:context="video.VideoActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takeVideo"
android:text="点击录像"></Button>
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vv"></VideoView>
</LinearLayout>
保存视频到本地
package video;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.VideoView;
import com.example.myapp_b.R;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SaveActivity extends AppCompatActivity {
VideoView vv;
static final int REQUEST_VIDEO_CAPTURE = 1;
static final int RESULT_OK=-1;
String path="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
vv=findViewById(R.id.vv);
}
public void takeVideo(View view) {
Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
String videoPath= Environment.getExternalStorageDirectory()+"/VIDEO";//设置照片保存路径
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file=new File(videoPath, "MP4_" + timeStamp + ".mp4");
path=file.getAbsolutePath();
//使用文件提供者获取路径URL
Uri contentUri= FileProvider.getUriForFile(this,"com.example.android.fileprovider",file);
//设置传递参数 让相机拍照后保存照片
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,contentUri);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//1.设置扫描意图
Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//2.设置路径
File f=new File(path);
//3.获取路径
Uri contenturi=Uri.fromFile(f);//file:///storage/emulated/0/VIDEO/MP4_20211201_213800.mp4
mediaScanIntent.setData(contenturi);
System.out.println(contenturi.toString());
//4.发送广播
this.sendBroadcast(mediaScanIntent);
/
//1.获取数据uri
super.onActivityResult(requestCode, resultCode, intent);
Uri videoUri = intent.getData();
//2.实例化媒体控制器
MediaController controller = new MediaController(this);
//3.设置播放源
vv.setVideoURI(videoUri);
//4.媒体控制器与视频控件相互关联
controller.setMediaPlayer(vv);
vv.setMediaController(controller);
}
}
添加权限
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="VIDEO" />
</paths>