我想在锁屏中添加拍照功能,我需要在屏幕上打开预览,但它一直失败,logcat说错误是关于“setPreviewWindow - Null ANativeWindow传递给setPreviewWindow”和“startPreview - 预览未启动。预览进行中标志集"
在数字时钟视图后的 keyguard_screen_tab_unlock.xml 中,把
<FrameLayout
android:id="@+id/mPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在 LockScreen.java 构造函数中:
FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.mPreview);
SurfacePreview preview = new SurfacePreview(mContext);
preview.setZOrderOnTop(true);
mFrameLayout.addView(preview);
SurfacePreview 类:
public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
SurfacePreview(Context context) {
super(context);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}
}
任何人都知道如何解决此问题并在 LockScreen 上启动 prview?非常感谢。