我正在尝试使用 xamarin 在图像视图中显示图像,但是当我按下相机中的接受按钮时,它不会返回到我的主要活动。
这是代码
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.AddPurchase);
// Create your application here
ConfigLayout ();
if (IsThereAnAppToTakePictures ())
{
CreateDirectoryForPictures ();
Button cameraButton = FindViewById<Button> (Resource.Id.buttonTakePicture);
_imageView = FindViewById<ImageView> (Resource.Id.imageView);
cameraButton.Click += TakeAPicture;
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Intent mediaScanIntent = new Intent (Intent.ActionMediaScannerScanFile);
var contentUri = Uri.FromFile (_file);
mediaScanIntent.SetData (contentUri);
SendBroadcast (mediaScanIntent);
int height = _imageView.Height;
int width = _imageView.Width;
using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height)) {
_imageView.SetImageBitmap (bitmap);
}
}
bool IsThereAnAppToTakePictures ()
{
Intent intent = new Intent (MediaStore.ActionImageCapture);
IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities (intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}
void CreateDirectoryForPictures ()
{
_dir = new File (Environment.GetExternalStoragePublicDirectory (Environment.DirectoryPictures), "Boekhoudservice");
if(!_dir.Exists ())
_dir.Mkdirs();
}
private void TakeAPicture(object sender, System.EventArgs eventArgs)
{
if (_imageView.Drawable != null) {
_imageView.SetImageResource (0);
}
Intent intent = new Intent (MediaStore.ActionImageCapture);
_file = new Java.IO.File(_dir, String.Format ("uitgave_{0}.jpg", System.DateTime.Now.ToString ("d")));
intent.PutExtra (MediaStore.ExtraOutput, Uri.FromFile(_file));
StartActivityForResult (intent, RequestCodeConstants.CameraIntent);
}
似乎问题出在我的目录创建方法上,在检查目录是否存在时,它会返回 true,而如果我自己检查,它不会出现在我的文件资源管理器中。
这两个权限都在我的 AndroidManifest 中
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />