Skip to content

Instantly share code, notes, and snippets.

@icebeam7

icebeam7/ServicioImagen.cs Secret

Created Dec 14, 2020
Embed
What would you like to do?
Servicios/ServicioImagen.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Essentials;
namespace ICTalkMobileApp.Services
{
public static class ServicioStorage
{
public static async Task<string> SeleccionarFoto()
{
var foto = await MediaPicker.PickPhotoAsync(new MediaPickerOptions()
{
Title = "Elige una imagen"
});
return await CopiarFoto(foto);
}
public static async Task<string> TomarFoto()
{
if (MediaPicker.IsCaptureSupported)
{
var foto = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions()
{
Title = "Toma la foto"
});
return await CopiarFoto(foto);
}
return string.Empty;
}
private static async Task<string> CopiarFoto(FileResult foto)
{
if (foto != null)
{
var nombreCopia = $"{Guid.NewGuid()}{Path.GetExtension(foto.FileName)}";
var rutaCopia = Path.Combine(FileSystem.CacheDirectory, nombreCopia);
using (var stream = await foto.OpenReadAsync())
{
using (var newStream = File.OpenWrite(rutaCopia))
{
await stream.CopyToAsync(newStream);
}
}
return rutaCopia;
}
return string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment