Galaxy S3 - taking picture in portrait mode destroys activity

纵然是瞬间 提交于 2019-11-29 17:22:14

问题


So, this question has been asked in many forms on this forum but none of the answers are helping me. I have a bug which I've only been able to reproduce on the Samsung Galaxy S3.

I want to simply take and store a photo using the phone's camera, but my Activity is being destroyed before I ever hear back from onActivityResult. This ONLY happens when I use the camera in portrait mode, in landscape mode it's fine.

I'm using this code to launch the camera:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, Config.ACTIVITY_TAKE_PHOTO);

In portrait mode, I never see a call to onActivityResult (from the Camera activity), I just see an onDestroy() called in my launching activity. There are no exceptions or errors in the log, and nothing obviously wrong to make it crash.

I've seen posts that recommend adding "android:configChanges="orientation|keyboardHidden"" to the Manifest, and doing lots of other things to manage the orientation change that occurs when using the camera, but nothing has helped so far.

Has anyone else solved this problem?


回答1:


If you are targeting beyond API level 13, Adding

android:configChanges="orientation|keyboardHidden"

to the Manifest will not be enough.

Check this extract from Android documentation

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

Hence try adding

android:configChanges="orientation|keyboardHidden|screenSize"

to your Manifest file. This should solve your problem.




回答2:


Android can call the destroy() method in certain scenarios. One is when we use the method finish(). An alternative is to use the method isFinishing () from Activity class. If it really is being finalized could finalize their resources, otherwise, you can keep the values ​​allocated a class that inherits Application

@Override
protected void onDestroy() {

    final YourApp app = (YourApp) getApplicationContext();
    if (app != null && isFinishing()) {
        app.finalizeAppResources();
    }

    // ...

    super.onDestroy();
}


来源:https://stackoverflow.com/questions/16154279/galaxy-s3-taking-picture-in-portrait-mode-destroys-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!