Taking picture from non-activity

前端 未结 3 842
时光取名叫无心
时光取名叫无心 2021-01-02 22:23

I am using dummy surface in my code.It\'s working fine in Canvas HD running 4.2.1 but when the same app is deployed on my nexus 5/S 3 it gives RunTimeException on camera.tak

相关标签:
3条回答
  • 2021-01-02 23:08

    This is not possible, you need an activity to display the camera in.

    Consider a transparent activity that just holds the camera view.

    0 讨论(0)
  • 2021-01-02 23:10

    You shouldn't use a dummy SurfaceView as Android checks is the SurfaceView is displayed on the screen.

    Instead, display the preview on a SurfaceView with WindowManager, with type SYSTEM_OVERLAY. Set your SurfaceView width and height to 1px and the opacity to 0 to hide the preview, and that's it!

    If you plan to support only 5.0+ devices, or it's a new app that won't be available too soon, you should consider using the new android Camera2 API, which fixed many mistakes of the older API.

    Hope it will help you :)

    0 讨论(0)
  • 2021-01-02 23:28

    You can use a dummy SurfaceTexture instead of a SurfaceView. This is reliable and works fine on the old camera API:

    mDummyTexture = new SurfaceTexture(1); // pick a random argument for texture id
    mCamera.setPreviewTexture(mDummyTexture);
    mCamera.startPreview();
    mCamera.takePicture(...);
    

    Just make sure to keep mDummyTexture as a member of your class, or you may get abandoned surface errors.

    As long as you don't call SurfaceTexture#updateTexImage(), you do not need an OpenGL context.

    0 讨论(0)
提交回复
热议问题