Xamarin Android - Take Picture From Camera Then Pass It To Other Activity
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 othe
Solution 1:
Try this:
protectedoverridevoidOnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
// It's a good idea that you check this before accessing the dataif (requestCode == 0 && resultCode == Result.Ok)
{
//get the image bitmap from the intent extrasvar 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 arraybyte[] 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:
protectedoverridevoidOnCreate(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 ByteArrayvar 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!
Post a Comment for "Xamarin Android - Take Picture From Camera Then Pass It To Other Activity"