在Xamarin Android中,试图从相机保存图像会引发NullReferenceException
我已经在这方面花费了太多的时间,在尝试从相机保存图像时无法解决此问题。我的代码如下: AndroidManifest.XML:在Xamarin Android中,试图从相机保存图像会引发NullReferenceException,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我已经在这方面花费了太多的时间,在尝试从相机保存图像时无法解决此问题。我的代码如下: AndroidManifest.XML: <application android:allowBackup="true" android:label="test" android:configChanges="orientation|screenSize"> <provider android:name="android.support.v4.content.FilePro
<application android:allowBackup="true" android:label="test" android:configChanges="orientation|screenSize">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
在这一行:
Bitmap imageBitmap = (Bitmap)extras.Get("data");
当我设置断点时,进入活动结果的
意图数据
看起来是空的。你能发布完整的堆栈跟踪吗?没有堆栈跟踪无法诊断此问题。另请参阅,谢谢您的回复。我正在努力获取堆栈跟踪。我没有提到的一件事是,当我设置断点时,进入活动结果的意图数据
似乎是空的。请确保您阅读了另一篇文章。NullReferenceException很容易修复。@fmaccaroni我将检查它。谢谢
namespace test
{
[Activity(Label = "@string/app_name", ScreenOrientation = ScreenOrientation.Portrait)]
public class AttachmentActivity : Activity
{
UserToken currentUser;
AuthAPI api;
UserDataAPI userdataApi;
OfflineCachingService offlineService;
public DeliveryForm deliveryReport { get; set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.deliveryReportAttachmentPage);
userdataApi = new UserDataAPI();
offlineService = new OfflineCachingService("Android");
currentUser = new UserToken();
api = new AuthAPI("Android");
currentUser = userdataApi.GetCurrentUser();
Button btnAddAttachment = FindViewById<Button>(Resource.Id.btnAddAttachment);
int cameraClicks = 0;
btnAddAttachment.Click += (sender, e) =>
{
Intent camera = new Intent(MediaStore.ActionImageCapture);
Java.IO.File photoFile = null;
try
{
photoFile = CreateImageFile();
}
catch(Exception error)
{
throw error;
};
if (photoFile != null)
{
Android.Net.Uri photoUri = FileProvider.GetUriForFile(this, "com.example.android.fileprovider", photoFile);
camera.PutExtra(MediaStore.ExtraOutput, photoUri);
}
cameraClicks++;
StartActivityForResult(camera, 1);
};
//grabs JSON inventory item from intent extra
string deliveryFormJson = Intent.GetStringExtra("SelectedDeliveryForm");
if (deliveryFormJson != null)
{
deliveryReport = JsonConvert.DeserializeObject<DeliveryForm>(deliveryFormJson);
}
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if (requestCode == 1 && resultCode == Android.App.Result.Ok)
{
Bundle extras = data.Extras;
Bitmap imageBitmap = (Bitmap)extras.Get("data");
ImageView imageView = FindViewById<ImageView>(Resource.Id.image);
imageView.SetImageBitmap(imageBitmap);
}
}
private Java.IO.File CreateImageFile()
{
String imageFileName = "attachment_" + Guid.NewGuid();
Java.IO.File storageDir = GetExternalFilesDir(Android.OS.Environment.DirectoryPictures);
Java.IO.File image = Java.IO.File.CreateTempFile(imageFileName, ".bmp", storageDir);
String photoPath = image.AbsolutePath;
return image;
}
}
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Bitmap imageBitmap = (Bitmap)extras.Get("data");