通过意图拍照后被杀死/onCreate 调用的活动 [英] Activity killed / onCreate called after taking picture via intent

查看:23
本文介绍了通过意图拍照后被杀死/onCreate 调用的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用意图拍照.我的问题是,有时在拍照后,我调用 startActivityForResult 的活动似乎被破坏了,因此再次调用 onCreate.

I am trying to take a picture using an intent. My problem is that sometimes after taking a picture my activity, which calls startActivityForResult, seems to be destroyed so that onCreate is called again.

这是我点击图片视图后拍照的代码,应该替换哪个图片:

Here is my code for taking pictures after clicking an imageview, which image should be replaced:

if (!getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            Util.makeLongToast(R.string.lang_no_camera);
        } else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_ITEM_PHOTO);
        }

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v(TAG, "onactivityresult called");
    if (requestCode == TAKE_ITEM_PHOTO) {
        if (data != null) {

            imageUri = data.getData();


                try {
                    img_photo.setImageBitmap(Media.getBitmap(
                            getContentResolver(), imageUri));
            } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        } else
            Log.w(TAG, "data is null");
    }
}

所以我尝试的只是拍照并用它替换图像视图的图像.但在某些情况下,onCreate 是在 onActivityResult 被调用之后被调用的,而新的图像会丢失.

So all i try is to take a picture and replace the image of an imageview with it. But in some cases onCreate is called after onActivityResult was called and the new image is lost.

非常感谢您的帮助.

推荐答案

实际上,相机会导致您的 Activity 中的方向发生变化,这就是您的 Activity 被销毁和重新创建的原因.

Actually the camera causes the orientation change in your activity that is why your activity is being destroyed and recreated.

将此添加到您的清单文件中,它将阻止方向更改,并且您的活动不会被破坏和重新创建.

Add this in your manifest file it will prevent the orientation change and your activity will not get destroyed and recreated.

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" >
</activity>

这篇关于通过意图拍照后被杀死/onCreate 调用的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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