当我尝试在Android上使用SurfaceView从相机进行预览时,为什么我的应用程序崩溃? 当我构建Android Studio没有错误。
这是代码:
public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback
{
private Camera camera;
static private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
surfaceView = (SurfaceView)findViewById(R.id.camera_surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder)
{
camera = Camera.open();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
camera.release();
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
try
{
camera.setPreviewDisplay(arg0);
camera.startPreview();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
有人可以帮助我吗?我会等你的回答。