c# - 为什么另一个activity中没有图片预览?
问题描述
我想捕获一张图片并在下一个活动中显示捕获的图片,但该图片没有显示在我的下一个活动中使用此代码。摄像头运行良好,可以跳转到下一个活动。
数据列表.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);
}
相机.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);
}
解决方案
using (new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, new MemoryStream());
bitmapData = new MemoryStream().ToArray();
}
你总是使用new MemoryStream()
,所以bitmapData
是byte[0]
;
像这样改变:
using ( var stream =new MemoryStream())
{
imageBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
推荐阅读
- python - 由于页面自动重新加载,使用 selenium 无限向下滚动总是失败
- node.js - NodeJS cors 不过滤任何东西
- r - r: Assign n dataframe subsets from a list created in a custom function to objects in global environment
- python - 数据框打印为空数据框?
- python - 如何使用python仅使用一个循环迭代二维矩阵的所有元素
- bash - 使用 cron 扩展参数
- azure - 部署在 Azure 中的 API 不起作用,我不知道为什么
- css - 使用背景滤镜时无法移除滚动上的模糊边缘:模糊
- c++ - 使用 Rcpp 函数作为符号地址传递的 NULL 值
- python - 使用 eval() 动态生成 Tkinter 子顶级类