2

I need to take more photos, but the app restarts on Android 11 devices. Android 10 or lower is OK. It behaves enigmatic. Sometimes it happens after third capturing, sometimes maybe after seventh capturing.

Simulation of the problem (restart after second capturing):

  1. Click Button for take a picture
  2. Take a picture
  3. Click OK
  4. App returns to the screen with button
  5. Steps 1-3
  6. App restarts and displays the home screen

If I place a breakpoint on var photo = await MediaPicker.CapturePhotoAsync(); and go to next step, nothing gets done and app crashes.

This also happens in App-Essentials Sample App.

Edit: My code:

var file = await MediaPicker.CapturePhotoAsync();

while (LS.IsIntermediateActivity())

{ await Task.Delay(10); }

Normal behaviour: capture photo, click OK, next step - while (LS.IsIntermediateActivity()) and then in MainActivity OnRestart and OnResume. (LS is DependencyService)

Incorrect behaviour: capture photo, click OK, no next step - app crash or restart and show MainPage. No Exception, no warning.

  • I have tested on pixel Android 11.0. I was not able to reproduce the app scrshes. I only checked with the code: `var photo = await MediaPicker.CapturePhotoAsync(); ` Am i miss something for this? – Wendy Zang - MSFT Sep 23 '21 at 06:49
  • You must be getting an exception. Figure out what it is and go from there. Perhaps you are not releasing some memory for the images and you run out of memory? Needs more info! – Cheesebaron Sep 23 '21 at 09:26
  • We are currently having a very similar error with essentials 1.7 on iOS 14.7.1. using that same await MediaPicker.CapturePhotoAsync(); When clicking the actual photo button inside the call, the app occasionally crashes and throws an exception that closes the app and isn't caught with a try/catch. We see this in AppCenter -> SIGABRT: Objective-C exception thrown. Name: NSGenericException Reason: *** Collection <__NSArrayM: 0x2839c0120> was mutated while being enumerated. – Dilbert789 Sep 23 '21 at 19:21
  • Can you share your code and some exception details if possible? – FreakyAli Oct 04 '21 at 06:04
  • No exception. That is the problem. – Martin Nebeský Oct 14 '21 at 11:41

4 Answers4

2

I remember one of my colleagues ran into a very similar issue and this note on Media picker that actually fixed their issue

All methods must be called on the UI thread because permission checks and requests are automatically handled by Xamarin.Essentials.

This is weird because their own example is not doing that :/

Anyway try this and see if this solves your issue:

var fileResult= await Device.InvokeOnMainThreadAsync(async () => await MediaPicker.CapturePhotoAsync());

Goodluck

Feel free to get back if you have queries.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
1

I looked into this very problem. Basically, when opening the camera with Media Picker from within you app, your app gets put in the background by Android since the camera app is now being used. Some RAM optimizations takes place and the OS may restart your app. In testing we had no problems with a device with 3Gb RAM, but run into this issue with a 2Gb RAM device. Very likely because of not enough RAM.

A solution would be to save your application state when leaving your app and reuse that after coming back from taking a photo, in case your app gets restarted. We haven't found a simple solution for that, ideas are very welcome...

Remo
  • 65
  • 5
0

I tried another solution:

public async Task<string> TakePhotoAsync()
    {
        waitForResult = new AsyncAutoResetEvent();
        var context = Android.App.Application.Context;
        var activity = Xamarin.Essentials.Platform.CurrentActivity as MainActivity;
        using (var intent = new Intent(MediaStore.ActionImageCapture))
        {
            string timestamp = System.DateTime.Now.ToString("yyyyMMdd_HHmmss");
            var file = new File(GetPictureDirPath(), "EMAphoto_" + timestamp + ".jpg");
            var uri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileprovider", file);
            intent.PutExtra(MediaStore.ExtraOutput, uri);
            activity.StartActivityForResult(intent, CAMERA_REQUEST_CODE);
            await waitForResult.WaitAsync().ConfigureAwait(true);
            return file.Path;
        }

    }

and it crashes too... Is stupid Android 11 or me?

0

Update to Android 12 and problem is solved...

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '22 at 16:54