0

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 ?

8
  • Check to see if any of the parameters passed to AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs() are null Commented Feb 7, 2018 at 6:33
  • @sme requestcode = 1000, resultcode = Android.App.Result.Canceled, data = null Commented Feb 7, 2018 at 7:14
  • It may be because data is null; Perhaps it requires the value to be non-null. Do a null check on data, and only call it if it is non-null (though I'll admin I'm not sure exactly what the function is doing) Commented Feb 7, 2018 at 7:17
  • @sme hmm as i expected , i tried to see the parameter after i login and the parameter totaly different with when i click the pick image , so when use this dependency service the parameter will be replaced from my dependency service parameter, hmm can you suggest me the code i should change so i dont have to change the paremeter? this code i got from developer.Xamarin, Commented Feb 7, 2018 at 7:27
  • my paremter when im not using the dependency service (this paremter i got from microsoft.identity.client / azure b2c) requestcode = 0 , resultcoded= Android.App.Result.Ok , data = {intent{act=return(has extras)}} Commented Feb 7, 2018 at 7:29

1 Answer 1

1

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 ?

OnActivityResult will be triggered when you call PublicClientApplication.AcquireTokenAsync and when you call StartActivityForResult. So the two parts logics should be separated according to the requestCode:

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);
        }
    }else
    {
      AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.