【问题标题】:Xamarin android - take picture from camera then pass it to other activityXamarin android - 从相机拍照然后将其传递给其他活动
【发布时间】:2017-03-14 10:54:55
【问题描述】:

我是 xamarin 的新手,我想在我的 mainactivity 上单击一个按钮时从相机拍摄一张照片,然后在拍摄完照片后,将其显示在另一个 Activity 的 imageView 中。

你能帮帮我吗?

这是我现在拥有的:

主要活动:

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

我不知道在 mainActivity 中的“PutExtra”上放什么才能在 AddFrais.cs 上创建位图...

感谢您的帮助!

【问题讨论】:

  • 您收到什么错误?
  • System.NullReferenceException:对象引用未设置为对象的实例。我不知道该向我的其他活动发送什么..

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


【解决方案1】:

试试这个:

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。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多