Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

IMvxPictureChooserTask.TakePicture/TakePictureAsync won´t launch camera on Marshmallow #23

Open
xleon opened this issue Oct 23, 2015 · 1 comment

Comments

@xleon
Copy link

xleon commented Oct 23, 2015

I was getting a Java.Lang.SecurityException when trying to use the picture chooser plugin:

Java.Lang.SecurityException: Exception of type 'Java.Lang.SecurityException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(.args:1337)
at Android.Runtime.JNIEnv.CallObjectMethod(IntPtr_jobject__IntPtr_jmethod__Android.Runtime.JValue*_parms.args:1337)
at Android.Content.ContentResolver.Insert(Android.Net.Uri_url__Android.Content.ContentValues_values.args:1337)
at com.yaesmio.onehour.dev.MvvmCross.Plugins.PictureChooser.Droid.MvxPictureChooserTask.GetNewImageUri(.args:1337)
at com.yaesmio.onehour.dev.MvvmCross.Plugins.PictureChooser.Droid.MvxPictureChooserTask.TakePicture(Int32_maxPixelDimension__Int32_percentQuality__System.Action`1_pictureAvailable__System.Action_assumeCancelled.args:1337)

After reading this post about a potential Marshmallow permissions bug: http://stackoverflow.com/questions/32789027/android-m-camera-intent-permission-bug

I tried this code before actually calling to IMvxPictureChooserTask.TakePictureAsync() (in the code sample it´s called by ViewModel.AttachFile())

In the activity:

public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Resource.Id.take_picture:
                try
                {
                    // this will throw if the system is not marshmallow
                    if (CheckSelfPermission(Manifest.Permission.Camera) != Permission.Granted)
                    {
                        RequestPermissions(new string[] { Manifest.Permission.Camera }, 1);
                    }
                    else
                    {
                        ViewModel.AttachFile(true);
                    }
                }
                catch (Exception)
                {
                    ViewModel.AttachFile(true);  // this works if android < 6.0
                }

                return true;

            case Resource.Id.take_picture_from_gallery:
                ViewModel.AttachFile(false);
                return true;
        }

        return base.OnOptionsItemSelected(item);
    }

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 1)
        {
            if (grantResults[0] == Permission.Granted)
            {
                ViewModel.AttachFile(true);  // this gets called after android permission prompt
            }
        }
    }

When this code runs, the system prompts if you want to give camera permission. But then nothing happens even if "ViewModel.AttachFile" is called. So I guess the problem could live in the intent used by the plugin to launch the camera.
I also tried with android.permission.CAMERA and without it (in AndroidManifest.xml), as some SO answers were suggesting it. Same result except that it will throw in android < 6.0.

I haven´t had the time to test this outside the plugin. That could give us a clue how to fix that in the plugin itself.

@Cheesebaron
Copy link
Member

This is related to #82

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants