Xamarin android - take picture from camera then pass it to other activity

柔情痞子 提交于 2021-01-28 05:27:04

问题


I am new to xamarin and I want to take a picture from the camera when I click on a button on my mainactivity and then, once the picture taken, display it in an imageView in an other activity.

Can you help me?

Here's what I have right now :

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);
        }
    }
}

I don't know what to put on the "PutExtra" in the mainActivity to be able to create a bitmap on AddFrais.cs...

Thanks for helping !


回答1:


Try this:

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...
}

Then in your second Activity:

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);
    }
}     

As you can see your second activity code is most likely the same, I just added a validation to prevent NullReferenceException if the image is not well extracted from the intent.

Hope this helps!



来源:https://stackoverflow.com/questions/42784071/xamarin-android-take-picture-from-camera-then-pass-it-to-other-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!