我需要拍一张照片并在位于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);
}
这有什么问题?
答案 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
。