Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created December 14, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/52c8ffde6b21a83614cb2a8a5e806d11 to your computer and use it in GitHub Desktop.
Save icebeam7/52c8ffde6b21a83614cb2a8a5e806d11 to your computer and use it in GitHub Desktop.
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