locked
How to take multiple pictures in Android before returning to Common code? RRS feed

  • Question

  • User234750 posted

    In a Xamarin Forms app, I have cribbed some code to take a picture. From a Common Page, I touch the "Take a Picture" button. That opens the native Android camera. I get to take one picture and am returned to the Common Page. I'd like to take several pictures, then indicate that I am done and return to the Common page and then do stuff with each of the pictures taken. I have thought of calling the TakePictureAsync method from within a while loop - but how do I end it? Is there a way to indicate that I am done from the native camera? Can I add a button over top of the native camera? Or do I need to put all the camera functionality into another page? Or can I create a page that has a Done button at the bottom and the rest of the page is the native camera?

    The following is the camera code I have borrowed. I am also open to considering other sources. Thanks!

    public class CameraProvider : ICameraProvider { private static File file; private static File pictureDirectory;

        private static TaskCompletionSource<CameraResult> tcs; 
    
        public Task<CameraResult> TakePictureAsync()
        {
            Intent intent = new Intent(MediaStore.ActionImageCapture);
    
            pictureDirectory = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");
    
            if (!pictureDirectory.Exists())
            {
                pictureDirectory.Mkdirs();
            }
    
            file = new File(pictureDirectory, String.Format("photo_{0}.jpg", Guid.NewGuid()));
    
            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(file));
    
            var activity = (Activity)Forms.Context;
            activity.StartActivityForResult(intent, 0);
    
            tcs = new TaskCompletionSource<CameraResult>();
    
            return tcs.Task;
        }
    
        public static void OnResult(Result resultCode)
        {
            if (resultCode == Result.Canceled)
            {
                tcs.TrySetResult(null);
                return;
            }
    
            if (resultCode != Result.Ok)
            {
                tcs.TrySetException(new Exception("Unexpected error"));
                return;
            }
    
            CameraResult res = new CameraResult();
            res.Picture = ImageSource.FromFile(file.Path);
            res.FilePath = file.Path; 
    
            tcs.TrySetResult(res);
        }
    }
    
    Wednesday, July 13, 2016 11:49 PM

All replies

  • User2223 posted

    Hello, I want to perform exactly the same, did you found a solution?

    Thank you in advance for your answer.

    J-D Gasser

    Monday, June 4, 2018 3:45 PM
  • User171749 posted

    @Jean-DanielGasser said: Hello, I want to perform exactly the same, did you found a solution?

    Thank you in advance for your answer.

    J-D Gasser

    I do this in an internal application I built. (This is using the Xamarin.Plugin.Media plugin).

    You might want to do something like the below

    ```CSHARP async Task AddPicture() { while (await AddOneMorePicture()) {} }

        async Task<bool> AddOneMorePicture()
        {
            try
            {
                var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    CompressionQuality = 100,
                    PhotoSize = PhotoSize.Small,
                    SaveToAlbum = false,
                    AllowCropping = false,
                    DefaultCamera = CameraDevice.Rear,
                });
    
                //LOGIC
                return true;
            }
            catch (Exception ex)
            {
                // HANDLE EXCEPTION
                return false;
            }
        }
    

    ```

    There is a "Cancel" button on the native camera App. When you want the user to stop taking photos press Cancel and the loop will stop.

    Monday, June 4, 2018 3:50 PM
  • User234750 posted

    @Jean-DanielGasser Hi. Unfortunately not. But maybe seanyda's answer will meet our needs.

    @seanyda Thanks for your response! I look forward to trying it out.

    Monday, June 4, 2018 3:56 PM
  • User2223 posted

    @"Will.Itrax" Thank you for the answer. I don't think @seanyda 's answer will do what I expect. I will search a solution further and post my solution in this thread if I find any.

    Monday, June 4, 2018 4:01 PM