Xamarin android - 从相机拍照然后将其传递给其他活动

时间:2017-03-14 10:54:55

标签: android android-intent xamarin camera xamarin.android

我是xamarin的新手,当我点击主动画上的按钮时,我想从相机拍照,然后,一旦拍完照片,将其显示在其他活动的imageView中。

你能帮帮我吗?

这就是我现在所拥有的:

MainActivity:

costsButton.Click += delegate
{
    Intent intent = new Intent(MediaStore.ActionImageCapture);
    StartActivityForResult(intent, 0);
};

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    var extra = data.GetByteArrayExtra("data");
    Intent intent = new Intent(this, typeof(AddFrais));

    intent.PutExtra("picture", extra);
    StartActivity(intent);
}

AddFrais.cs:

namespace Projet_stage_2017
{
    [Activity(Label = "AddFrais")]
    public class AddFrais : Activity
    {
        ImageView picturefrais;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.AddFrais);
            picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais);
            var image = Intent.GetByteArrayExtra("picture") ?? null;
            Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);

            picturefrais.SetImageBitmap(bitmap);
        }
    }
}

我不知道该怎样放在&#34; PutExtra&#34;在mainActivity中能够在AddFrais.cs上创建位图......

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

试试这个:

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

    // It's a good idea that you check this before accessing the data
    if (requestCode == 0 && resultCode == Result.Ok)
    { 
        //get the image bitmap from the intent extras
        var image = (Bitmap)data.Extras.Get("data");

        // you might also like to check whether image is null or not
        // if (image == null) do something

        //convert bitmap into byte array
        byte[] bitmapData;
        using (var stream = new MemoryStream())
        {
            image.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        Intent intent = new Intent(this, typeof(AddFrais));

        intent.PutExtra("picture", bitmapData);

        StartActivity(intent);
    }
    // if you got here something bad happened...
}

然后在你的第二个活动中:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.AddFrais);

    picturefrais = FindViewById<ImageView>(Resource.Id.ImageFrais);

    //Get image from intent as ByteArray
    var image = Intent.GetByteArrayExtra("picture");

    if (image != null)
    { 
        //Convert byte array back into bitmap
        Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);
        picturefrais.SetImageBitmap(bitmap);
    }
}     

正如您所看到的,您的第二个活动代码很可能是相同的,我只是添加了一个验证来防止NullReferenceException,如果图像没有从意图中提取得好。

希望这有帮助!