为什么其他活动中没有图片预览?

时间:2019-07-16 08:33:11

标签: c# android xamarin.android

我想捕获图片并在下一个活动中显示捕获的图片,但是此代码不会在我的下一个活动中显示。相机运行良好,可以跳至下一个活动。

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

1 个答案:

答案 0 :(得分:0)

view = inflater.inflate(R.layout.mylayout, null);

您始终使用 using (new MemoryStream()) { imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, new MemoryStream()); bitmapData = new MemoryStream().ToArray(); } ,所以new MemoryStream()bitmapData;

更改如下:

byte[0]