Reputation: 7
I want to capture a picture and display captured picture on next activity but the picture is not shown in my next activity with this code. The camera is working well and it can jump to next activity.
datalist.cs
btnCamera.Click += delegate
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(intent, 0);
};
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == Result.Ok)
{
Bitmap imageBitmap = (Bitmap)data.Extras.Get("data");
byte[] bitmapData;
using (new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, new MemoryStream());
bitmapData = new MemoryStream().ToArray();
}
Intent intent = new Intent(this, typeof(camera));
intent.PutExtra("picture", bitmapData);
StartActivity(intent);
}
camera.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.image_upload);
upload = FindViewById<Button>(Resource.Id.upload);
pic = FindViewById<ImageView>(Resource.Id.camera);
if (Intent.GetByteArrayExtra("picture") != null)
{
//Convert byte array back into bitmap
Bitmap bitmap =
BitmapFactory.DecodeByteArray(Intent.GetByteArrayExtra("picture"), 0,
Intent.GetByteArrayExtra("picture").Length);
pic.SetImageBitmap(bitmap);
}
Upvotes: 0
Views: 54
Reputation: 14981
using (new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, new MemoryStream());
bitmapData = new MemoryStream().ToArray();
}
you always use new MemoryStream()
,so bitmapData
is byte[0]
;
change like this:
using ( var stream =new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
Upvotes: 0