Xamarin Android:将数据发送到外部 activity 并从结果中检索它们

Xamarin Android: Sending data to an external activity and retrieving them from the result

我正在尝试使用某些参数调用相机,以及我的 activity 需要处理返回的照片的一些额外数据, 我使用了这段代码:

Intent intent = new Intent(MediaStore.ActionImageCapture);
Java.IO.File file = new Java.IO.File(System.IO.Path.Combine(App.DatabaseFolder, ATTACHMENT_FILE));
intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file));
intent.PutExtra(MediaStore.ExtraScreenOrientation, (int)ScreenOrientation.Portrait);
intent.PutExtra("source_id", id); // here I put the extra data
StartActivityForResult(intent, CAMERA_INTENT_REQUEST);

现在我正尝试在结果返回时检索发送到 activity 的 ID

有办法吗?我试过了

if (requestCode = CAMERA_INTENT_REQUEST && resultCode == Result.Ok) {
    data.GetIntExtra("source_id", -1);
}

但我似乎无法检索结果

作为解决方法,我设法通过在结果代码或文件名中保存 id 来完成上述操作,但我更愿意找到另一种方法,有吗?

如果您觉得代码很奇怪,它是用 Xamarin 编写的 Android,但它仍然是一个 android 问题

提前感谢您提供的任何帮助

你不能那样做。相机是一个外部应用程序,不会收集您的 "extra info".

更多信息在这里:AndroidDeveloer - Camera API

所以在你的情况下,你应该将你的 id 存储到全局变量或 SharedPreferences