Xamarin Forms - 从相机拍摄照片(Android)

时间:2018-01-24 19:52:05

标签: forms xamarin camera capture

拍照时,相机的回调永远不会被调用。它只会在我从图库中选择现有照片时调用。

MessagingCenter.Subscribe<byte[]>(this, "ImageSelected", (args) =>
    {
        Device.BeginInvokeOnMainThread(() =>
        {
        //Set the source of the image view with the byte array   <<== NEVER INVOKED 
        Image img = new Image();

        MemoryStream stream = new MemoryStream((byte[])args);

        //img.Source = ImageSource.FromStream(() => new MemoryStream((byte[])args));

        imagePallet.Source = ImageSource.FromStream(() => { return stream; });

        imagePreview.IsVisible = true;

        });
    });

我正在使用此处列出的设置: https://baglabs.com/2017/01/12/xamarin-forms-using-camera-photo-gallery/

我注意到OnActivityResult在MainActivity.cs中执行,但是当从相机中选择照片时,Intent的Data属性始终为null,只有在像以前一样从库中选择照片时才设置它。

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    //Since we set the request code to 1 for both the camera and photo gallery, that's what we need to check for
    if (requestCode == 1)
    {
        if (resultCode == Result.Ok)
        {
            if (data.Data != null)  --<<==ALWAYS NULL WHEN ACCEPTING PHOTO TAKEN WITH CAMERA
            {
                //Grab the Uri which is holding the path to the image
                Android.Net.Uri uri = data.Data;

                //Read the meta data of the image to determine what orientation the image should be in
                int orientation = getOrientation(uri);

                //Stat a background task so we can do all the bitmap stuff off the UI thread
                BitmapWorkerTask task = new BitmapWorkerTask(this.ContentResolver, uri);
                task.Execute(orientation);
            }


        }
    }

显然我错过了一些东西。非常感谢!

0 个答案:

没有答案