I am using Xamarin Forms
in which I implemented the MediaPLugin plugin to take a picture.
The detail is that when I take the photo from my device it sends me the following error:
Unhandled Exception:
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView () on the child's parent first.
I leave my code of how I have my XAML
<telerikInput:RadEntry x:Name="et_Cliente"
WatermarkText="Ingresar Nombre de Cliente"
WidthRequest="50"
FontSize="16"
BorderStyle="{StaticResource EntryBorderStyle}" >
</telerikInput:RadEntry>
<telerikInput:RadEntry x:Name="et_telefono"
WatermarkText="Ingresar Numero de Telefono"
WidthRequest="50"
FontSize="16"
BorderStyle="{StaticResource EntryBorderStyle}">
</telerikInput:RadEntry>
<telerikInput:RadEntry x:Name="et_folioContrato"
WatermarkText="Ingresar Folio de Contrato"
WidthRequest="50"
FontSize="16"
BorderStyle="{StaticResource EntryBorderStyle}">
</telerikInput:RadEntry>
<telerikInput:RadEntry x:Name="et_Direccion"
WatermarkText="Ingresar Direccion"
WidthRequest="50"
FontSize="16"
BorderStyle="{StaticResource EntryBorderStyle}">
</telerikInput:RadEntry>
<telerikGrid:RadDataGrid x:Name="RG_Detalle"
AutoGenerateColumns="False"
SelectionMode="Multiple"
UserEditMode="Cell"
UserFilterMode="Disabled"
UserGroupMode="Disabled"
SelectionUnit="Row">
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridNumericalColumn PropertyName="Cantidad" HeaderText="Cantidad" CanUserEdit="True"></telerikGrid:DataGridNumericalColumn>
<telerikGrid:DataGridTextColumn PropertyName="Descripcion" HeaderText="Descripcion" CanUserEdit="False"/>
<telerikGrid:DataGridNumericalColumn PropertyName="PrecioVenta" HeaderText="Precio" CanUserEdit="True" CellContentFormat="{}{0:N2}" />
</telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>
<Label x:Name="TotalAplicado" HorizontalOptions="EndAndExpand" TextColor="Red" FontSize="15" ></Label>
<Button x:Name="takePhoto" Text="Foto INE Delantera" Image="camera.png" MinimumWidthRequest="200"/>
<Label x:Name="lbl_inedelantera" FontSize="20" FontAttributes="Bold" TextColor="DarkRed"></Label>
<Image x:Name="ine_delantera" MinimumWidthRequest="100" WidthRequest="100" Aspect="AspectFill"></Image>
<!--<telerikInput:RadButton x:Name="btn_inetrasera" Text="Foto INE Trasera" Image="camera.png" MinimumWidthRequest="500"/>
<Label x:Name="lbl_inetrasera" FontSize="20" FontAttributes="Bold" TextColor="DarkRed"></Label>
<Image x:Name="ine_trasera" MinimumWidthRequest="500" Aspect="AspectFill" ></Image>
<telerikInput:RadButton x:Name="btn_contrato" Text="Foto Contrato" Image="camera.png" MinimumHeightRequest="50"/>
<Label x:Name="lbl_contrato" FontSize="20" FontAttributes="Bold" TextColor="DarkRed"></Label>
<Image x:Name="img_contrato" MinimumWidthRequest="500" Aspect="AspectFill" ></Image>
<telerikInput:RadButton x:Name="btn_comprobantedomicilio" Text="Foto Comprobante" Image="camera.png" MinimumWidthRequest="50" />
<Label x:Name="lbl_comprobante" FontSize="20" FontAttributes="Bold" TextColor="DarkRed"></Label>
<Image x:Name="img_comprobante" MinimumWidthRequest="500" Aspect="AspectFill"></Image>-->
<telerikInput:RadButton x:Name="Grabar" Text="Grabar" CornerRadius="15" BorderRadius="10" Clicked="Grabar_Clicked"></telerikInput:RadButton>
</StackLayout>
</ScrollView>
And I also leave code of how I implement it in the .cs
takePhoto.Clicked += async (sender, args) =>
{
try
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( Camara nos Disponible.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
//Directory = "Test",
Directory = "ONControl",
Name = "ine_delantera.jpg",
SaveToAlbum = true,
CompressionQuality = 75,
CustomPhotoSize = 50,
PhotoSize = PhotoSize.Small,
MaxWidthHeight = 300,
DefaultCamera = CameraDevice.Front,
});
if (file == null)
{
return;
}
// await DisplayAlert("Imagen Guarda", file.Path, "OK");
using (var memoryStream = new MemoryStream())
{
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
var pictures = dir.AbsolutePath;
string filePath = System.IO.Path.Combine(pictures, "ine_delantera.jpg");
byte[] ImageDat;
file.GetStream().CopyTo(memoryStream);
ImageDat = memoryStream.ToArray();
File.WriteAllBytes(filePath, ImageDat);
lbl_inedelantera.Text = "INE DELANTERA OK";
this.ine_delantera.Source = ImageSource.FromFile(file.AlbumPath);
}
}
catch (Exception e)
{
await DisplayAlert("Error", e.Message.ToString(), "Aceptar");
}
};
The weird thing is that sometimes I do not get the error, but I could not find the detail