【发布时间】:2018-12-10 04:15:41
【问题描述】:
让这个活动允许我拿起或选择一部手机,然后裁剪它并将其设置为 ImageView。 然而,在图像被裁剪后,它在 Bitmap bitmap = (Bitmap)bundle.GetParcelable("data"); 上返回 null 也试过 Bitmap bitmap = (Bitmap)data.extras.Get("data"); 这是我的代码
private void CameraOpen()
{
CamIntent = new Intent(MediaStore.ActionImageCapture);
file = new File(Android.OS.Environment.ExternalStorageDirectory, "file_" + Guid.NewGuid().ToString() + ".jpg");
uri = Android.Net.Uri.FromFile(file);
CamIntent.PutExtra(MediaStore.ExtraOutput, uri);
CamIntent.PutExtra("return-data", true);
StartActivityForResult(CamIntent, 0);
}
private void GaleryOpen()
{
GalIntent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
StartActivityForResult(Intent.CreateChooser(GalIntent, "Select image from galery"), 2);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if(requestCode == 0 && resultCode == Result.Ok)
{
CropImage();
}
else if (requestCode == 2)
{
if (data != null)
{
uri = data.Data;
CropImage();
}
}
else if (requestCode == 1)
{
if (data != null)
{
Bundle bundle = data.Extras;
Bitmap bitmap = (Bitmap)bundle.GetParcelable("data");
imgPhoto.SetImageBitmap(bitmap);
}
}
}
private void CropImage()
{
try
{
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.SetDataAndType(uri, "image/*");
CropIntent.PutExtra("crop", "true");
CropIntent.PutExtra("outputX", 180);
CropIntent.PutExtra("outputY", 180);
CropIntent.PutExtra("aspectX", 4);
CropIntent.PutExtra("aspectY", 4);
CropIntent.PutExtra("scaleUpIfNeeded", true);
CropIntent.PutExtra("return-data", "true");
StartActivityForResult(CropIntent, 1);
}
catch (ActivityNotFoundException ex)
{
}
}
出现错误
未处理的异常:
System.NullReferenceException:
非常感谢您的帮助。
【问题讨论】:
标签: android xamarin bitmap xamarin.android onactivityresult