0

I have spent too much time on this already and can't get past this issue when trying to save an image from the camera. My code is as follows:

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>

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

}

When I run the application and launch the camera, I can snap a picture and am presented with a preview and a checkmark to keep the image or an 'x' to try again. When I tap on the checkmark, the app throws the following exception:

Unhandled Exception:

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

on this line:

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

The intent data that is going into the OnActivityResult appears to be empty when I set a break point.

  • Can you post the full stack trace? There's no way to diagnose this without the stack trace. – Robert Harvey Aug 03 '18 at 15:03
  • See also [What is a NullReferenceException and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Robert Harvey Aug 03 '18 at 15:04
  • Thanks for your response. I am working on getting the stack trace. One thing that I failed to mention is the `intent data` that is going into the `OnActivityResult` appears to be empty when I set a break point. – Leon Lovett Aug 03 '18 at 15:18
  • Make sure you read that other post. NullReferenceException is pretty straightforward to fix. – Robert Harvey Aug 03 '18 at 15:19
  • Even though IDK what is the solution to that error, you can use https://github.com/jamesmontemagno/MediaPlugin to do that much simpler – fmaccaroni Aug 03 '18 at 15:35
  • 1
    @fmaccaroni I will check it out. Thanks! – Leon Lovett Aug 03 '18 at 15:54

0 Answers0