Camera API working on Jelly Bean but not Kitkat

后端 未结 3 442
自闭症患者
自闭症患者 2020-12-30 13:55

I have a really strange problem. The following code I have is used to take a picture on button click. It works properly on Jelly Bean phones, but not on Kitkat:

3条回答
  •  有刺的猬
    2020-12-30 14:20

    KitKat's garbage collection works differently than previous APIs.

    I'm guessing that the PhotoHandler object that you pass to the takePicture() method is getting garbage collected before onPictureTaken can be called.

    Try making a PhotoHandler object as in instance variable in your MainActivity.

    At the top of the class:

    PhotoHandler photoHandler;
    

    Then in onCreate()

    photoHandler = new PhotoHandler(getApplicationContext());
    

    Then when you call takePicture():

    camera.takePicture(null, null, photoHandler);
    

提交回复
热议问题