Skip to content

Instantly share code, notes, and snippets.

@dimonovdd
Last active October 17, 2020 13:50
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 dimonovdd/07ecb6ceb14bf72c7e54dc0210e4d0c0 to your computer and use it in GitHub Desktop.
Save dimonovdd/07ecb6ceb14bf72c7e54dc0210e4d0c0 to your computer and use it in GitHub Desktop.
SaveToGalery
public async Task<bool> SaveImageAsync(byte[] data, string filename, string folder = null)
{
try
{
File picturesDirectory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
File folderDirectory;
if (await Permissions.RequestAsync<Permissions.StorageWrite>() != PermissionStatus.Granted)
return false;
if (!string.IsNullOrEmpty(folder))
{
folderDirectory = new File(picturesDirectory, folder);
folderDirectory.Mkdirs();
}
else
folderDirectory = picturesDirectory;
using (File bitmapFile = new File(folderDirectory, filename))
{
bitmapFile.CreateNewFile();
using (FileOutputStream outputStream = new FileOutputStream(bitmapFile))
await outputStream.WriteAsync(data);
MediaScannerConnection.ScanFile(Platform.CurrentActivity,
new string[] { bitmapFile.Path },
new string[] { "image/png", "image/jpeg" }, null);
}
}
catch
{
return false;
}
return true;
}
public Task<bool> SaveImageAsync(byte[] data, string filename, string folder = null)
{
NSData nsData = NSData.FromArray(data);
UIImage image = new UIImage(nsData);
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
image.SaveToPhotosAlbum((UIImage img, NSError error) => taskCompletionSource.SetResult(error == null));
return taskCompletionSource.Task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment