Я новичок в Android Studio. Я делаю приложение Google Glass для записи видео. Я получил ошибку «Приложение потеряло поверхность». Я пробовал некоторые решения, но я не мог решить эту ошибку.
Это мой код приложения Google Glass.
import java.io.File;
import java.io.IOException;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
@SuppressLint("Registered")
public class VideoActivity extends Activity {
String fileOutput = "DCIM/Camera/imgtest.mp4";
String path = Environment.getExternalStorageDirectory() + File.separator + fileOutput;
private android.hardware.Camera mCamera;
//private Camera mCamera;
private MediaRecorder mediaRecorder;
/*SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try
{
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mCamera != null)
{
try {
mCamera.setPreviewDisplay(holder);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(640, 360);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null)
{
mCamera.stopPreview();
//mCamera = null;
}
}
};*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
SurfaceView mPreview = (SurfaceView) findViewById(R.id.preview);
mCamera = Camera.open();
Recording(mPreview);
if (mCamera != null) {
mCamera.release();
}
}
private void Recording (SurfaceView view)
{
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
try {
mediaRecorder = new MediaRecorder();
SurfaceHolder mHolder = view.getHolder();
//mHolder.addCallback(surfaceCallback);
Surface surface = mHolder.getSurface();
//SurfaceTexture surfaceTexture = new SurfaceTexture(0);
mediaRecorder.setCamera(mCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mediaRecorder.setMaxDuration(1000);
mediaRecorder.setCaptureRate(15);
mediaRecorder.setVideoFrameRate(15);
//mCamera.setPreviewTexture(surfaceTexture);
mediaRecorder.setPreviewDisplay(surface);
mediaRecorder.setOutputFile(path);
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Это код ошибки.
E/MediaRecorderJNI: Application lost the surface
W/System.err: java.io.IOException: invalid preview surface
at android.media.MediaRecorder._prepare(Native Method)
at android.media.MediaRecorder.prepare(MediaRecorder.java:739)
at com.example.glass.transfer1.VideoActivity.Recording(VideoActivity.java:102)
at com.example.glass.transfer1.VideoActivity.onCreate(VideoActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5236)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Я не знаю, почему появилась эта ошибка. Я добавил SurfaceHolder и Surface в Записи, но приложение потеряло поверхность. Как я могу исправить эту ошибку? Спасибо.