jamesmontemagno / MediaPlugin

Take & Pick Photos and Video Plugin for Xamarin and Windows
MIT License
711 stars 360 forks source link

Issue only in Android Emulator but worried it could happen on other models - Java Cast Exception - Looping or Calling Camera Twice #874

Open dbassioneisdata opened 4 years ago

dbassioneisdata commented 4 years ago

Bug Information

Version Number of Plugin: 5.0.1 Device Tested On: Andorid, IOS , Andorid Emulator, IOS Emulator Version of VS: 2019 Version 16.6.3 Version of Xamarin: 16.6.000.1062 Versions of other things you are using:

THIS was not an issue until last week

Steps to reproduce the Behavior

When calling the CrossMedia.Current.TakePhotoAsync in a loop Calling the CrossMedia.Current.TakePhotoAsync again

Expected Behavior

Loop around and open the camera up again to take a picture

Actual Behavior

Crashes with the following message

Java.Lang.ClassCastException Message=android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

Code snippet

private async Task CameraCall() { bool p_stayInLoop = true;

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            return "NO CAMERA";
        }

        while (p_stayInLoop)
        {
            EisPhotoRoll p_tempPicture = new EisPhotoRoll();
            p_tempPicture.azurekey = Guid.NewGuid().ToString();
            p_tempPicture.filename = p_tempPicture.azurekey + ".png";

            MediaFile file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "WAPLINK",
                Name = p_tempPicture.filename,
                CompressionQuality = 75,
                CustomPhotoSize = 50,
                PhotoSize = PhotoSize.MaxWidthHeight,
                MaxWidthHeight = 2000,
                DefaultCamera = CameraDevice.Rear
            });

            if (file == null)
            {
                p_stayInLoop = false;
            }
            else
            {
                p_tempPicture.picture = file;
                p_returnRoll.Add(p_tempPicture);
            }
        }

        if (p_returnRoll.Count == 0)
        {
            return "NO PHOTO";
        }
        return "PHOTO";
    }

Or calling with no loop

private async Task CameraCall() { bool p_stayInLoop = true;

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            return "NO CAMERA";
        }

            EisPhotoRoll p_tempPicture = new EisPhotoRoll();
            p_tempPicture.azurekey = Guid.NewGuid().ToString();
            p_tempPicture.filename = p_tempPicture.azurekey + ".png";

            MediaFile file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "WAPLINK",
                Name = p_tempPicture.filename,
                CompressionQuality = 75,
                CustomPhotoSize = 50,
                PhotoSize = PhotoSize.MaxWidthHeight,
                MaxWidthHeight = 2000,
                DefaultCamera = CameraDevice.Rear
            });

            if (file != null)
            {
                p_tempPicture.picture = file;
                p_returnRoll.Add(p_tempPicture);         
            }

        if (p_returnRoll.Count == 0)
        {
            return "NO PHOTO";
        }
        return "PHOTO";
    }

Screenshots

dbassioneisdata commented 4 years ago

UPDATE - It works fine on new Android Units but on older ones we are getting the same

Java.Lang.ClassCastException Message=android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

Error