尝试从相机保存图像会在Xamarin Android中引发NullReferenceException

时间:2018-08-03 15:01:17

标签: android xamarin xamarin.android

我已经花了太多时间在此上,尝试从相机保存图像时无法解决此问题。我的代码如下:

AndroidManifest.XML:

<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>

资源/xml/file_paths.xml:

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/test.Droid_New2/files/Pictures" />
</paths>

AttachmentActivity.cs:

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;
    }
}

}

当我运行该应用程序并启动相机时,我可以拍摄一张照片,并显示预览和对勾以保留图像,或者显示“ x”以再次尝试。当我点击对勾标记时,该应用会引发以下异常:

Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

在此行:

Bitmap imageBitmap = (Bitmap)extras.Get("data");

设置断点时,进入intent data的{​​{1}}似乎是空的。

0 个答案:

没有答案