Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin.Forms Android App Restarts after Camera taking photo

AnbuMani27AnbuMani27 USMember ✭✭✭
edited June 2020 in Xamarin.Forms

I have a Xamarin. Forms application, and when the application starts everything working fine. After that, I want to take and store the photo using our phone's camera. Here the problem is when I was the open camera and taking the photo, After I click the save button it is not returning OnActivityResult Method. The camera that will be crashing the MainActivity.cs, Then XF is restarted and the app starts on the MainPage.

Xamarin.Forms Android - v3.5.0.274416

MainActivity.cs Code :-

      BtnCamera.ShouldTakePicture += () =>
                    {
                       var intent = new Intent(MediaStore.ActionImageCapture);
                        var filePath = CreateFilePath.GetFilePath();
                        Java.IO.File file;

                            file = new Java.IO.File(filePath, TruckDetails.TripID +  Guid.NewGuid().ToString() +".png");

                        (new Java.IO.File(filePath)).SetWritable(true, false);
                        intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(file));
                         StartActivityForResult(intent, 100);
            };

PS :-
1. I am not used MediaPlugin DLL.
2. It is not happening every time.

App Center Log -:

    Android: 9
    Android Build: PPOS29.114-134-7-7
    Manufacturer: motorola
    Model: moto g(7) power
    CrashReporter Key: 1e8720f8-b0fb-42b5-bf1c-d6466eb00140
    Start Date: 2020-06-29T03:01:10.721Z
    Date: 2020-06-29T03:01:12.758Z

    Xamarin Exception Stack:
    System.NullReferenceException: Object reference not set to an instance of an object
      at Scoular.Droid.MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00240] in <4dd616694d1a48ffb7992593dad90b4a>:0
      at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <ff07eae8184a40a08e79049bbcb31a0e>:0
      at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <b62c3fba3fe848f3bb323dbe33464223>:0
      at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <b62c3fba3fe848f3bb323dbe33464223>:0
      at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <b62c3fba3fe848f3bb323dbe33464223>:0
      at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.29(intptr,intptr

Thanks advance :)

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited June 2020

    Xamarin Exception Stack:
    System.NullReferenceException: Object reference not set to an instance of an object
    at Scoular.Droid.MainActivity.OnActivityResult (System.Int32 requestCode, Android.App.Result resultCode, Android.Content.Intent data) [0x00240] in <4dd616694d1a48ffb7992593dad90b4a>:0

    From the error log you posted, we could find a NullReferenceException(Object reference not set to an instance of an object), so there should be a variable there and you haven't initial a value for it. You can recheck it.

    If it is convenient for you, could you please share the code of method OnActivityResult of MainActivity or a basic demo which will help us to address the problem?

  • AnbuMani27AnbuMani27 USMember ✭✭✭
    edited June 2020

    Hi @jezh, Because of this NULL error, All Existing instance are removed in the stacks.

    My Mobile Log:
    Step 1: OnSleep : Camera is Opened
    Step 2: OnStart : App Started
    Step 3: OnActivityResult: NULL Error

  • jezhjezh Member, Xamarin Team Xamurai

    According to your post log, it's hard to locate the problem,could you please share a basic demo so that we can try to reproduce this question on our side?

  • AnbuMani27AnbuMani27 USMember ✭✭✭

    Sure @jezh

Sign In or Register to comment.