Hi I tried to implement ImagePicker on my xamarin forms but i have issues on my MainActivty OnActivityResult, whenever i choose the image it will got this error message
android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.Identity.Client.WebUI.SetAuthorizationResult (Microsoft.Identity.Client.Internal.AuthorizationResult authorizationResultInput) [0x00006] in <df3bbcf06538443e9963d82dd707b6fa>:0
at Microsoft.Identity.Client.AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00032] in <df3bbcf06538443e9963d82dd707b6fa>:0
at KGVC.Droid.MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x0000b] in <e83f90a066ab4f6c95402217456751e2>:0
at Android.Support.V4.App.FragmentActivity.n_OnActivityResult_IILandroid_content_Intent_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 requestCode, System.Int32 native_resultCode, System.IntPtr native_data) [0x00014] in <7a2a36256f1648ecbd0c15a75bc5a349>:0
at (wrapper dynamic-method) System.Object:409b2b82-0807-4c59-b475-d61eb538da78 (intptr,intptr,int,int,intptr)
at md5926298ec23f3b6e841a6fb18f139a084.MainActivity.n_onActivityResult(Native Method)
at md5926298ec23f3b6e841a6fb18f139a084.MainActivity.onActivityResult(MainActivity.java:39)
at android.app.Activity.dispatchActivityResult(Activity.java:6533)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3919)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3966)
at android.app.ActivityThread.access$1500(ActivityThread.java:180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5795)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:768)
my first though about this problem is because resultcode for my AzureB2c got replaced by resultcode from my PicturePickerImplementation, its because when i try to remove this code AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data); from my OnActivityResult it will works as charm, my question is how to fix this conflict ?
here is my full code
public static readonly int PickImageId = 1000;
public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
if (requestCode == PickImageId)
{
if ((resultCode == Result.Ok) && (data != null))
{
Android.Net.Uri uri = data.Data;
Stream stream = ContentResolver.OpenInputStream(uri);
// Set the Stream as the completion of the Task
PickImageTaskCompletionSource.SetResult(stream);
}
else
{
PickImageTaskCompletionSource.SetResult(null);
}
}
}
here is my DependencyService
using System;
using System.IO;
using System.Threading.Tasks;
using Android.Content;
using Xamarin.Forms;
using KGVC.Droid;
using KGVC.Interfaces;
[assembly: Dependency(typeof(PicturePickerImplementation))]
namespace KGVC.Droid
{
public class PicturePickerImplementation : IPicturePicker
{
public Task<Stream> GetImageStreamAsync()
{
// Define the Intent for getting images
Intent data = new Intent();
data.SetType("image/*");
data.SetAction(Intent.ActionGetContent);
// Get the MainActivity instance
MainActivity activity = Forms.Context as MainActivity;
// Start the picture-picker activity (resumes in MainActivity.cs)
activity.StartActivityForResult(
Intent.CreateChooser(data, "Select Picture"),
MainActivity.PickImageId);
// Save the TaskCompletionSource object as a MainActivity property
activity.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();
// Return Task object
return activity.PickImageTaskCompletionSource.Task;
}
}
}
any suggestion to fix my problem ? thanks EDIT: after a little observation, i notice some strange behavior that trigger this error. My error will only apears after i succesfull login and then i'm close the application and kill it from background, and to make my picker image work what should i do is 1.Make My App run on background 2.Relogin Again and i can use image picker again. Anyone has experience this issue ?
AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs()are nulldata, and only call it if it is non-null (though I'll admin I'm not sure exactly what the function is doing)