0

最初にカメラの意図を送信するアクティビティがあります。ユーザーに写真を撮ってもらいたいので、それを使って何かをします。私の問題は、ユーザーが写真を撮っている間に回転を変更すると、単一のデバイスの向きにとどまりながらプロセス全体を完了するまで、アプリがカメラ内でループし続けることです。

コードは次のとおりです。

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 ();
    }

私の問題は、アクティビティが再作成されてから、カメラ アプリを再度起動することです。私は多くのことを試しました(これはクリーンバージョンです)が、完璧に機能するものはありませんでした...

何か案は?

4

3 に答える 3

0

Slack Shots に追加するには、アクティビティのライフ サイクル内で何が起こっているかを理解する必要があるため、アプリの向きをロックして変更を防止するか、より良い方法 (状態を保存する) か、向きの変更を直接処理する必要があります。

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

于 2013-08-14T12:53:55.777 に答える
0

これはまさにあなたが探していた解決策ではないかもしれませんが、アクティビティ中に画面の回転を単純にロックしてみてください。

Android マニフェストで次のように設定できます。

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

これにより、デバイスが回転したときにアクティビティが再描画されなくなります。アクティビティに横長のレイアウトの方が適していると思われる場合は、明らかに「縦長」を「横長」に置き換えることができます。

于 2013-08-12T19:25:03.600 に答える
0

アドバイスをする前に、アクティビティの状態を質問のように保存することをお勧めします。

残念ながら、私は失敗しました。: )

状態を保存

于 2013-08-13T02:56:33.717 に答える