ConstraintLayout Visibility在使用相机拍照后无法显示

时间:2017-12-06 05:19:50

标签: xamarin.android imageview android-camera visibility android-constraintlayout

我需要拍一张照片并在位于SelectedImageView内的SelectedImageConstraintLayout中显示。当我打开图库并选择一张图片时,它可以很好地工作,但它不适用于相机。 SelectedImageConstraintLayout完全不可见。

void OpenCamera()
{
    Intent intent = new Intent(MediaStore.ActionImageCapture);
    var filename = new File(this.ViewModel.IssuePhotosDirPath,
                            this.ViewModel.TEMP_PHOTO_FILENAME);
    intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(filename));
    StartActivityForResult(intent, OPEN_CAMERA_ID);
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (resultCode == Result.Ok)
    {
        var issuePhotoFilename = System.IO.Path.Combine(this.ViewModel.IssuePhotosDirPath,
                                                        this.ViewModel.TEMP_PHOTO_FILENAME);

        switch(requestCode)
        {
            case OPEN_CAMERA_ID:
                ShowIssueImage(issuePhotoFilename);
                break;
            case OPEN_GALLERY_ID:
                //do something to save the image in temp file before showing it
                ShowIssueImage(issuePhotoFilename);
                break;
        }
    }
}

void ShowIssueImage(string fileName)
{
     if (SelectedImageViewWidth > 0 && SelectedImageViewHeight > 0)
     {
          var bitmap = fileName.ResizeAndRotate(SelectedImageViewWidth, SelectedImageViewHeight);
          SelectedImageView.SetImageBitmap(bitmap);
          bitmap = null;
     }
     else
     {  
          SelectedImageView.SetImageBitmap(BitmapFactory.DecodeFile(fileName));
     }

     AddImageConstraintLayout.Visibility = ViewStates.Gone;
     SelectedImageConstraintLayout.Visibility = ViewStates.Visible;
}

void RemoveIssueImage()
{
     SelectedImageConstraintLayout.Visibility = ViewStates.Gone;
     AddImageConstraintLayout.Visibility = ViewStates.Visible;

     var issuePhotoFilename = System.IO.Path.Combine(this.ViewModel.IssuePhotosDirPath, this.ViewModel.TEMP_PHOTO_FILENAME);
     System.IO.File.Delete(issuePhotoFilename);
}

这有什么问题?

1 个答案:

答案 0 :(得分:0)

我发现使用ViewState.Invisible代替ViewStates.Gone解决它。虽然我不知道为什么ViewState.Gone适用于从图库中选择图像。

void RemoveIssueImage()
{
     //Here, I need to change it from ViewStates.Gone to ViewStates.Invisible
     SelectedImageConstraintLayout.Visibility = ViewStates.Invisible;

     AddImageConstraintLayout.Visibility = ViewStates.Visible;

     var issuePhotoFilename = System.IO.Path.Combine(this.ViewModel.IssuePhotosDirPath, this.ViewModel.TEMP_PHOTO_FILENAME);
     System.IO.File.Delete(issuePhotoFilename);
}

以及稍后在ShowIssueImage函数中,将AddImageConstraintLayout.Visibility更改为ViewStates.Invisible