C# 为什么其他活动中没有图片预览?
我想捕获一张图片并在下一个活动中显示捕获的图片,但此代码未在下一个活动中显示该图片。摄像机工作正常,可以跳转到下一个活动 datalist.csC# 为什么其他活动中没有图片预览?,c#,android,xamarin.android,C#,Android,Xamarin.android,我想捕获一张图片并在下一个活动中显示捕获的图片,但此代码未在下一个活动中显示该图片。摄像机工作正常,可以跳转到下一个活动 datalist.cs btnCamera.Click += delegate { Intent intent = new Intent(MediaStore.ActionImageCapture); StartActivityForResult(intent, 0); }; } prot
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);
}
您总是使用新的MemoryStream,因此bitmapData是字节[0]
像这样改变:
using ( var stream =new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
意图对象将绑定到调用活动的上下文,因此它在新活动中不可用。您需要将其传递。此行:Bitmap-imageBitmap=Bitmapdata.Extras.Getdata;您只能显示低质量的预览,您需要通过文件URI访问存储并加载图片您可以向我显示示例代码吗?否。图像仍然无法传递到新活动您可以在运行时进行调试,查看bitmapData是否为空,我在我这方面对其进行测试,它可以正常工作
using ( var stream =new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}