android – 如果相机旋转,来自相机的图像为null

我正在向用户显示一个选择器,用于在图库或相机之间进行选择以选择照片.如果我选择相机,那么一旦相机加载我旋转拍摄风景照片,拍照并点击完成,它返回我的应用程序但返回的图像为空.
如果我不旋转相机,图像将返回正常.
我错过了什么?
我知道旋转导致重建Activity,但为什么onActivityResult不包含正确的信息?
这是我的open Image意图:
public void openImageIntent() {

        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyAppImages"
                + File.separator);
        root.mkdirs();
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy-hhmmss");
        final String fname = String.format("%s.jpg",sdf.format(new Date()));
        final File sdImageMainDirectory = new File(root,fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
        for (ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent,"Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,cameraIntents.toArray(new Parcelable[] {}));

        startActivityForResult(chooserIntent,SELECT_PICTURE_REQUEST);
    }

和onActivityResult方法

protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE_REQUEST) {
                final boolean isCamera;
                if (data == null) {
                    isCamera = true;
                } else {
                    final String action = data.getAction();
                    if (action == null) {
                        isCamera = false;
                    } else {
                        isCamera = true;
                    }
                }

                Uri selectedImageUri;
                if (isCamera) {
                    selectedImageUri = outputFileUri;
                } else {
                    selectedImageUri = data == null ? null : data.getData();
                }

                if (imageDelegate != null) {
                    Log.e(TAG,"imageDelegate not null: " + imageDelegate);
                    imageDelegate.gotNewImageUri(selectedImageUri);
                    imageDelegate = null;

                } else if (getSupportFragmentManager().findFragmentByTag("addofferdialog") != null) {
                    imageDelegate = (AddOfferFragment) getSupportFragmentManager().findFragmentByTag("addofferdialog");
                    Log.e(TAG,"imageDelegate is null but found fragment: " + imageDelegate);
                    Log.e(TAG,"Activity image: " + selectedImageUri);
                    imageDelegate.gotNewImageUri(selectedImageUri);
                    imageDelegate = null;
                } else {
                    Log.e(TAG,"cannot find imageDelegate!!!!");
                }

             Log.e(TAG,"selectedImageUri: " + selectedImageUri);
            }
        }
    }

解决方法

你必须改变你的清单文件

在您的清单中只需替换下面的代码

<activity android:name=".CameraTestActivity"
              android:label="@string/app_name"     android:configChanges="keyboardHidden|orientation">

用你的代码

<activity android:name=".CameraTestActivity"
          android:label="@string/app_name">

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...