Camera capture issues on Android N

Part of my app uses the following code to capture an image from the camera of the device. This has been working fine until using the app on an Android N device. I understand there have been some permission changes to passing URI's in this version but I'm struggling to find the correct solution.

Any help would be appreciated.
'

    // Photo capture button click handler
    private void TakeAPicture(object sender, EventArgs eventArgs)
    {
        Intent intent = new Intent(MediaStore.ActionImageCapture);
        StartActivityForResult(intent, 0);
    }

    // Post photo capture handler
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (data == null) return;

        var dataData = data.Data;
        using (var bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, dataData)) // <-- Crashes here as the Data from the intent is null
        {
            Bitmap photoResized = BitmapHelpers.ScaleDown(bitmap, GlobalContants.ImageMaxPix);
            var isPortrait = BitmapHelpers.IsPortrait(ContentResolver, MediaStore.Images.ImageColumns.Orientation, dataData);
            using (photoResized = BitmapHelpers.Rotate(photoResized, isPortrait))
            {
                if (photoResized != null)
                {
                    byte[] fullPhotoBytes = BitmapHelpers.ConvertBitmapToBytes(photoResized);
                    using (Bitmap thumbnail = BitmapHelpers.ScaleDown(bitmap, GlobalContants.ImageThumbnailPix))
                    {
                        var thumbnailBytes = BitmapHelpers.ConvertBitmapToBytes(thumbnail);
                        int savedToLocalDatabase = DocumentManager.SaveDocument(Globals.UserId, Globals.UserEmail, Globals.CompanyKey, Globals.ProjectId, Globals.PropertyId, GlobalContants.PropertyJobItemId, Globals.CurrentLongitude, Globals.CurrentLatitude, _propertyReference, fullPhotoBytes, thumbnailBytes);
                        bool propertyPhotoUpdated = DocumentManager.UpdateDocumentNotToDelete(Globals.CompanyKey, Globals.UserEmail, Globals.ProjectId, Globals.PropertyId, GlobalContants.PropertyJobItemId);
                        if (savedToLocalDatabase > 0 && propertyPhotoUpdated)
                            Task.Run(async () => await new SynchronisationManager().SynchroniseDocuments(IsReachable()));
                    }
                }
            }
        }
        if (requestCode == 0 && resultCode == Result.Ok)
            ContentResolver.Delete(dataData, null, null);
        GC.Collect();
    }`

Answers

  • The_PeterThe_Peter SEMember
    edited December 2017

    Hi David, try this instead, works for me on Android N:

    data.Extras.Get("data") is where youll find the image.

    if(data.Extras.IsEmpty == false)
    {
    Bitmap theImage = (Bitmap)data.Extras.Get("data");
    continue with code.....
    }

  • The_PeterThe_Peter SEMember
    edited December 2017

    And dont forget to ask for permissions if not already granted:
    if (ActivityCompat.CheckSelfPermission((Android.App.Activity)Forms.Context, Manifest.Permission.Camera) != (int)Permission.Granted)
    {
    ActivityCompat.RequestPermissions((Android.App.Activity)Forms.Context, new String[] { Manifest.Permission.Camera }, (int)Permission.Granted);
    }

  • DavidMeddingsDavidMeddings USMember ✭✭

    Hi The_Peter,

    Thanks for your quick response!

    Apologies, I posted recently edited code as I was previously using (Bitmap)data.Extras.Get("data"); which does work. The issue with using that is that it doesn't seem to be a full-size image, just a thumbnail.

  • The_PeterThe_Peter SEMember

    Try add MediaStore.ExtraOutput to the intent, think that will do it.

  • DevLawDevLaw Member ✭✭

    @The_Peter said:
    Try add MediaStore.ExtraOutput to the intent, think that will do it.

    How do I add this to the intent?

Sign In or Register to comment.