Android 带摄像头活动的处理设备旋转

Android 带摄像头活动的处理设备旋转,android,android-activity,xamarin.android,Android,Android Activity,Xamarin.android,我有一个活动,它做的第一件事就是发送一个摄像头。我希望用户拍摄一张照片,然后我将使用它做一些事情。我的问题是,如果用户在拍照时改变旋转方向,应用程序会在相机内不断循环,直到他完成整个过程,同时保持在单个设备方向 代码如下: protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); _dateTextView = FindViewById<TextView>

我有一个活动,它做的第一件事就是发送一个摄像头。我希望用户拍摄一张照片,然后我将使用它做一些事情。我的问题是,如果用户在拍照时改变旋转方向,应用程序会在相机内不断循环,直到他完成整个过程,同时保持在单个设备方向

代码如下:

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        _dateTextView = FindViewById<TextView> (Resource.Id.DateLabel);
        ViewModel.RecomendDishViewModel.RecomendationSent += RecomendationSent;
        if (IsThereAnAppToTakePictures())
        {
            CreateDirectoryForPictures();
            var imageButton = FindViewById<ImageButton> (Resource.Id.TakeImageWaterMark);
            _imageView = FindViewById<ImageView> (Resource.Id.UserDishImage);
            imageButton.Click += TakePictureClicked;
            if(_bitmap != null)
            {
                _imageView.RecycleBitmap ();
                _imageView.SetImageBitmap(_bitmap);
            }
        }
        _dateTextView.Click += DateLabelClicked;
        TakePictureClicked ()
    }
protected void TakePictureClicked ()
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);
        _file = new Java.IO.File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));
        intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
        StartActivityForResult(intent, 0);
    }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        // make it available in the gallery
        Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
        Uri contentUri = Uri.FromFile(_file);
        mediaScanIntent.SetData(contentUri);
        SendBroadcast(mediaScanIntent);

        // display in ImageView. We will resize the bitmap to fit the display
        // Loading the full sized image will consume to much memory 
        // and cause the application to crash.
        _bitmap = _file.Path.LoadAndResizeBitmap (518, 388);
        if(_bitmap != null)
        {
            MemoryStream stream = new MemoryStream();
            _bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            byte[] bitmapData = stream.ToArray();
            ViewModel.RecomendDishViewModel.ImageData = bitmapData;
            if(_imageView == null)
            {
                return;
            }
            _imageView.RecycleBitmap ();
            _imageView.SetImageBitmap(_bitmap);
        }
        _file.Dispose ();
        _dir.Dispose ();
    }
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
_dateTextView=FindViewById(Resource.Id.DateLabel);
ViewModel.RecomendDishViewModel.RecomendationSent+=RecomendationSent;
如果(IsThereAnaptoTakePictures())
{
CreateDirectoryForPictures();
var imageButton=findviewbyd(Resource.Id.TakeImageWaterMark);
_imageView=findviewbyd(Resource.Id.UserDishImage);
imageButton.Click+=TakePictureClicked;
如果(_位图!=null)
{
_imageView.reyclebitmap();
_SetImageBitmap(_bitmap);
}
}
_dateTextView.Click+=DateLabelClicked;
TakePictureClicked()
}
受保护的无效文件已被删除()
{
意向意向=新意向(MediaStore.ActionImageCapture);
_file=newjava.IO.file(_dir,String.Format(“myPhoto_{0}.jpg”,Guid.NewGuid());
intent.PutExtra(MediaStore.ExtraOutput,Uri.FromFile(_file));
StartActivityForResult(意向,0);
}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、意图数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
//让它在图库中可用
Intent mediaScanIntent=新的Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri=Uri.FromFile(_文件);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
//在ImageView中显示。我们将调整位图大小以适合显示
//加载全尺寸图像将消耗大量内存
//并导致应用程序崩溃。
_位图=_file.Path.LoadAndResizeBitmap(518388);
如果(_位图!=null)
{
MemoryStream stream=新的MemoryStream();
_Compress(bitmap.CompressFormat.Png,0,流);
字节[]位图数据=stream.ToArray();
ViewModel.RecomendDishViewModel.ImageData=位图数据;
如果(_imageView==null)
{
返回;
}
_imageView.reyclebitmap();
_SetImageBitmap(_bitmap);
}
_Dispose();
_dir.Dispose();
}
我的问题是,活动被重新创建,然后再次启动摄像头应用程序。我已经尝试了很多东西(这是干净的版本),但没有什么工作完美


有什么想法吗?

这可能不是您想要的解决方案,但您可以尝试在活动期间锁定屏幕旋转

您可以在Android清单中这样设置:

<activity android:name="MyActivity" 
android:screenOrientation="portrait">
...
</activity>

...

这将防止在设备旋转时重新绘制活动。显然,如果您认为横向布局更适合您的活动,您可以将“纵向”替换为“横向”。

我建议将活动状态保存为我之前给出建议的问题

遗憾的是,我失败了


要添加到松弛快照,请回答您真正需要了解活动生命周期内发生的事情,您需要锁定应用程序的方向并防止更改,或者使用更好的方法(保存状态)或直接处理方向更改