Мое приложение падает с этим сообщением об ошибке. Я не знаю, как установить выходной файл для этого кода. Пожалуйста, помогите мне исправить мой код. Я очень новичок в android.
I / FrontAndBackCamera: подготовка не удалась I / System.out: java .io.IOException: нет действительного выходного файла I / FrontAndBackCamera: начать запись
Вот моя MainActivity. java
enter code here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dual_cam);
recordBtn=findViewById(R.id.button);
recordBtn.setOnClickListener(recordVideoListener);
Log.i(TAG,"Number of cameras:" + Camera.getNumberOfCameras());
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},CAMERA);
}
else {
Log.i(TAG,"Already have the permission");
mBackCamera=getCameraInstance(0);
BackCameraPreview mBackCamPreview = new BackCameraPreview(this, mBackCamera);
FrameLayout backPreview=(FrameLayout) findViewById(R.id.back_camera_preview);
backPreview.addView(mBackCamPreview);
mFrontCamera=getCameraInstance(1);
FrontCameraPreview mFrontCamPreview = new FrontCameraPreview(this, mFrontCamera);
FrameLayout frontPreview=(FrameLayout) findViewById(R.id.front_camera_preview);
frontPreview.addView(mFrontCamPreview);
Log.i(TAG,"about to call prepare to record video...");
prepareToRecordVideo();
}
}
public void prepareToRecordVideo()
{
//start recording stuff
Log.i(TAG,"PREPARE TO RECORD VIDEO");
mBackCamera.unlock();
mFrontCamera.unlock();
mediaRecorder1.setCamera(mBackCamera);
mediaRecorder2.setCamera(mFrontCamera);
mediaRecorder1.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder1.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder1.setProfile(CamcorderProfile.get(0,CamcorderProfile.QUALITY_HIGH));
mediaRecorder1.setAudioSamplingRate(8000);
mediaRecorder1.setOutputFile(this.initFile("CAMERA_1_").toString());
sft=new SurfaceTexture(0);
sf=new Surface(sft);
mediaRecorder1.setPreviewDisplay(sf);
mediaRecorder1.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mediaRecorder, int i, int i1) {
Log.i(TAG,"RECORDING FAILED ERROR CODE:"+i+"AND EXTRA CODE:"+i1);
}
});
CamcorderProfile cp=CamcorderProfile.get(1,CamcorderProfile.QUALITY_HIGH);
mediaRecorder2.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder2.setOutputFormat(cp.fileFormat);
mediaRecorder2.setVideoEncoder(cp.videoCodec);
mediaRecorder2.setVideoEncodingBitRate(cp.videoBitRate);
mediaRecorder2.setVideoFrameRate(cp.videoFrameRate);
mediaRecorder2.setVideoSize(cp.videoFrameWidth,cp.videoFrameHeight);
mediaRecorder2.setOutputFile(this.initFile("CAMERA_2_").toString());
sft2=new SurfaceTexture(0);
sf2=new Surface(sft2);
mediaRecorder2.setPreviewDisplay(sf2);
mInitSuccesful=true;
Log.i(TAG,"RECORD INIT SUCCESSFUL"+ mInitSuccesful);
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch(requestCode){
case CAMERA:
{
if(grantResults.length > 0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
mBackCamera=getCameraInstance(0);
mFrontCamera=getCameraInstance(1);
Log.i(TAG,"about to call prepareToRecordVideo...");
prepareToRecordVideo();
}else
{
Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT).show();
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
private View.OnClickListener recordVideoListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if (recording) {
mediaRecorder1.stop();
mediaRecorder1.reset();
mediaRecorder2.stop();
mediaRecorder2.reset();
recording = false;
recordBtn.setText("Start Recording");
Log.i(TAG, "Stop Recording");
} else {
//start recording
try {
mediaRecorder1.prepare();
mediaRecorder2.prepare();
} catch (IOException e) {
Log.i(TAG, "Prepare failed");
System.out.println(""+e);
}
catch(IllegalStateException e){
Log.i(TAG, "Start Recording cam1");
mediaRecorder1.start();
Log.i(TAG, "Start Recording cam2");
mediaRecorder2.start();
}
recording = true;
recordBtn.setText("Stop Recording");
Log.i(TAG, "Start Recording");
}
}
};
private File initFile (String cameraNamePrefix)
{
File dir=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"FrontAndBackCamera");
if(!dir.exists()&& !dir.mkdirs())
{
Log.wtf(TAG,"Failed to create storage directory:"+dir.getAbsolutePath());
Toast.makeText(this, "not record", Toast.LENGTH_SHORT).show();
file=null;
}
else{
String timeStamp=new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date());
File mediaFile;
mediaFile=new File(dir.getPath()+File.separator+cameraNamePrefix+timeStamp+".mp4");
Log.i(TAG,mediaFile.getAbsolutePath());
return mediaFile;
}
return file;
}
public static Camera getCameraInstance(int cameraId)
{
Camera camera =null;
try{
camera=Camera.open(cameraId);
}
catch (Exception e){
Log.e(TAG,"Camera" + cameraId+"is not available"+e.toString());
}
return camera;
}