反观从相机意向崩溃活动 [英] Coming back from Camera Intent crashes Activity

查看:120
本文介绍了反观从相机意向崩溃活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动一个摄像头意图是这样,没什么特别的活动:

I have an Activity that launches a Camera Intent like this (nothing special):

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  filename);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    Uri imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, CAMERA_REQUEST);

比我得到的结果 onActivityResult(),并设置位图到一个ImageView的。再次,没什么特别的。

Than I get the result in onActivityResult() and set the bitmap to an ImageView. Again, nothing special.

的问题是,在相机应用总是在景观。因此,如果用户器件保持在水平方向时,点击确定把它送回来给我的活动,我的活动是pviously $ P $的肖像,那么它崩溃我的活动,因为它必须重建。如果你倾斜设备为纵向您挖掘相机确定之前,那么它不会崩溃。我该如何解决这个问题?

The problem is that the Camera app is always in landscape. So if the user keeps the device in the horizontal orientation when they hit OK to send it back to my Activity, and my Activity was previously in portrait, then it crashes my activity because it has to rebuild it. If you tilt the device to portrait orientation before you tap OK in the camera, then it does not crash. How do I get around this?

推荐答案

您需要重写的onSaveInstanceState 方法适合转动之前,要坚持你的活动的设置。然后,在onCreate方法,您可以检查,看看是否捆绑参数为空。如果不能,那么你正在重新创建活动,并应通过其加载保存的设置。

You need to override the onSaveInstanceState method to persist the settings of your activity before fit rotates. Then, in the onCreate method, you can check to see if the Bundle argument is null. If not, then you are recreating your activity and should load the saved settings from it.

这篇关于反观从相机意向崩溃活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆