Forum Xamarin Cross Platform with Xamarin

Emtpy exception when taking photo using CrossMedia Plugin

Hi Guys,

I'm working on a mobile Android app using Xamarin Forms and Visual Studio.

I'm using the CrossMedia Plugin to be able to take or select photo's in my mobile app. At first I had problems with the initialize and that issue appeared to be caused by the wrong Android SDK I was targeting. After I updated the SDK and updated all the packages I was able to get the 'select a photo' option working, but using the camera still doesn't work, and I can't figure out what is causing this.

I've got the following method;

private async void TakeAPhoto(object sender, EventArgs e)
        {
            try
            {
                await CrossMedia.Current.Initialize();
            }
            catch (Exception exception)
            {
                await DisplayAlert("ERROR", "Error initializing camera!", "Ok");
            }

            var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
            var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

            if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
                cameraStatus = results[Permission.Camera];
                storageStatus = results[Permission.Storage];
            }

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No camera", "No camera available", "Ok");
                return;
            }

            if (cameraStatus == PermissionStatus.Granted && storageStatus == PermissionStatus.Granted)
            {
                MediaFile file;
                try
                {
                    //Exception occurs in this code.
                    file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                    {
                        //Specify Store to Album OR Directory, not both
                        Directory = "App_Images",
                        Name = "Test.jpg"

                    });
                }
                catch (Exception exception)
                {
                    //I've got a break point here which is being hit, but the exception is (null)
                    throw;
                }

                if (file == null)
                    return;

                //TODO: Store image to azure.
            }
            else
            {
                await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");
                //On iOS you may want to send your user to the settings screen.
                //CrossPermissions.Current.OpenAppSettings();
            }
        }

The exception occurs in the 'TakeAPhotoAsync'-method, but unfortunately it's '(null)', so that leaves me with nothing.
However, the output window in visual studio gives me the following information;

08-21 15:18:27.882 I/MonoDroid( 4411): UNHANDLED EXCEPTION:
08-21 15:18:27.923 I/MonoDroid( 4411): Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in :0
08-21 15:18:27.923 I/MonoDroid( 4411): at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in :0
08-21 15:18:27.923 I/MonoDroid( 4411): at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00077] in <5cd30cd1648349a38d9dd27b51ad75f2>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at Plugin.Media.MediaPickerActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x0023f] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaPickerActivity.cs:162
08-21 15:18:27.923 I/MonoDroid( 4411): --- End of stack trace from previous location where exception was thrown ---
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 08-21 15:18:27.923 I/MonoDroid( 4411): at Plugin.Media.MediaImplementation+<TakePhotoAsync>d__16.MoveNext () [0x000c7] in C:\projects\mediaplugin\src\Media.Plugin.Android\MediaImplementation.cs:119 08-21 15:18:27.923 I/MonoDroid( 4411): --- End of stack trace from previous location where exception was thrown --- 08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at MyFirstApp.Views.ListViewPage+d__7.MoveNext () [0x00516] in D:\Projects\MyFirstApp App\Dev\MyFirstApp\MyFirstApp\MyFirstApp\Views\ListViewPage.xaml.cs:103
08-21 15:18:27.923 I/MonoDroid( 4411): --- End of stack trace from previous location where exception was thrown ---
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
08-21 15:18:27.923 I/MonoDroid( 4411): at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 () [0x00000] in :0
08-21 15:18:27.923 I/MonoDroid( 4411): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0
08-21 15:18:27.923 I/MonoDroid( 4411): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in :0
08-21 15:18:27.923 I/MonoDroid( 4411): at (wrapper dynamic-method) System.Object:496909f7-d190-419d-833e-380c474d206f (intptr,intptr)
08-21 15:18:27.923 I/MonoDroid( 4411): --- End of managed Java.Lang.NullPointerException stack trace ---
08-21 15:18:27.923 I/MonoDroid( 4411): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:27.923 I/MonoDroid( 4411): at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
08-21 15:18:27.923 I/MonoDroid( 4411): at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
08-21 15:18:27.923 I/MonoDroid( 4411): at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
08-21 15:18:27.923 I/MonoDroid( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.n_onCreate(Native Method)
08-21 15:18:27.924 I/MonoDroid( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.Activity.performCreate(Activity.java:6237)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.ActivityThread.-wrap11(ActivityThread.java)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.os.Looper.loop(Looper.java:148)
08-21 15:18:27.924 I/MonoDroid( 4411): at android.app.ActivityThread.main(ActivityThread.java:5417)
08-21 15:18:27.924 I/MonoDroid( 4411): at java.lang.reflect.Method.invoke(Native Method)
08-21 15:18:27.924 I/MonoDroid( 4411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
08-21 15:18:27.924 I/MonoDroid( 4411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-21 15:18:27.930 D/Mono ( 4411): DllImport searching in: '__Internal' ('(null)').
08-21 15:18:27.930 D/Mono ( 4411): Searching for 'java_interop_jnienv_throw'.
08-21 15:18:27.930 D/Mono ( 4411): Probing 'java_interop_jnienv_throw'.
08-21 15:18:27.930 D/Mono ( 4411): Found as 'java_interop_jnienv_throw'.
08-21 15:18:28.640 E/mono ( 4411):
08-21 15:18:28.640 E/mono ( 4411): Unhandled Exception:
08-21 15:18:28.640 E/mono ( 4411): Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:28.640 E/mono ( 4411): --- End of managed Java.Lang.NullPointerException stack trace ---
08-21 15:18:28.640 E/mono ( 4411): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:28.640 E/mono ( 4411): at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
08-21 15:18:28.640 E/mono ( 4411): at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
08-21 15:18:28.640 E/mono ( 4411): at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
08-21 15:18:28.640 E/mono ( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.n_onCreate(Native Method)
08-21 15:18:28.640 E/mono ( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
08-21 15:18:28.640 E/mono ( 4411): at android.app.Activity.performCreate(Activity.java:6237)
08-21 15:18:28.640 E/mono ( 4411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
08-21 15:18:28.640 E/mono ( 4411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
08-21 15:18:28.640 E/mono ( 4411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
08-21 15:18:28.640 E/mono ( 4411): at android.app.ActivityThread.-wrap11(ActivityThread.java)
08-21 15:18:28.640 E/mono ( 4411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
08-21 15:18:28.640 E/mono ( 4411): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 15:18:28.640 E/mono ( 4411): at android.os.Looper.loop(Looper.java:148)
08-21 15:18:28.640 E/mono ( 4411): at android.app.ActivityThread.main(ActivityThread.java:5417)
08-21 15:18:28.640 E/mono ( 4411): at java.lang.reflect.Method.invoke(Native Method)
08-21 15:18:28.640 E/mono ( 4411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
08-21 15:18:28.640 E/mono ( 4411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-21 15:18:28.640 E/mono ( 4411):
08-21 15:18:28.641 E/mono-rt ( 4411): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:28.641 E/mono-rt ( 4411): --- End of managed Java.Lang.NullPointerException stack trace ---
08-21 15:18:28.641 E/mono-rt ( 4411): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
08-21 15:18:28.641 E/mono-rt ( 4411): at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
08-21 15:18:28.641 E/mono-rt ( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.n_onCreate(Native Method)
08-21 15:18:28.641 E/mono-rt ( 4411): at md5f1b52d9f1d5d1981437e7e130649468a.MediaPickerActivity.onCreate(MediaPickerActivity.java:42)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.Activity.performCreate(Activity.java:6237)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.ActivityThread.-wrap11(ActivityThread.java)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.os.Looper.loop(Looper.java:148)
08-21 15:18:28.641 E/mono-rt ( 4411): at android.app.ActivityThread.main(ActivityThread.java:5417)
08-21 15:18:28.641 E/mono-rt ( 4411): at java.lang.reflect.Method.invoke(Native Method)
08-21 15:18:28.641 E/mono-rt ( 4411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
08-21 15:18:28.641 E/mono-rt ( 4411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-21 15:18:28.641 E/mono-rt ( 4411):
referenceTable GDEF length=814 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47302 1
referenceTable GDEF length=808 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=49128 1
referenceTable head length=54 1
referenceTable head length=54 1

And this includes the exception as well, only I don't know how to fix this issue. Anybody any ideas?

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @RobLohmann,

    I had a blank exception case in past, only what I did was ---> uninstalled the plug-in from all projects (pcl/droid/ios) under my solution and removed the obj folder from project directory. Ran the cleanup (visual studio command) and built/executed the application for once, after running once in emulator -reinstalled the plug-in using latest stable nuget and registered as per the documentation once again.

    That worked for me, did not had a time to look what caused the issue at that time (since the exception was not telling anything and couldn't have the time to review LogCat at that time).

    Hope it helps.
    N Baua

  • RohemRohem USMember ✭✭

    @N_Baua said:
    Hi @RobLohmann,

    I had a blank exception case in past, only what I did was ---> uninstalled the plug-in from all projects (pcl/droid/ios) under my solution and removed the obj folder from project directory. Ran the cleanup (visual studio command) and built/executed the application for once, after running once in emulator -reinstalled the plug-in using latest stable nuget and registered as per the documentation once again.

    That worked for me, did not had a time to look what caused the issue at that time (since the exception was not telling anything and couldn't have the time to review LogCat at that time).

    Hope it helps.
    N Baua

    Hi @N_Baua,

    Sorry for the late response, was busy with other stuff. Thank you for your suggestion, I'll try this later this week.

  • RohemRohem USMember ✭✭

    @N_Baua said:
    Hi @RobLohmann,

    I had a blank exception case in past, only what I did was ---> uninstalled the plug-in from all projects (pcl/droid/ios) under my solution and removed the obj folder from project directory. Ran the cleanup (visual studio command) and built/executed the application for once, after running once in emulator -reinstalled the plug-in using latest stable nuget and registered as per the documentation once again.

    That worked for me, did not had a time to look what caused the issue at that time (since the exception was not telling anything and couldn't have the time to review LogCat at that time).

    Hope it helps.
    N Baua

    Unfortunately I'm still getting the emtpy exception.. Any other ideas?

  • RohemRohem USMember ✭✭

    Problem finally solved. I had the Compiling version and target version of the android SDK higher then described here. Once I had set them exactly to the values mentioned in the documentation everything worked.

Sign In or Register to comment.