PCL Save Image File to local File System

flo1411 picture flo1411 · Mar 19, 2014 · Viewed 14.5k times · Source

I am looking for a way to store image files in my local filesystem using the PCL Storage plugin in my core Project for Windows Phone, Xamarin.Android and Xamarin.iOS. However, the plugin does just provide methods for writing Text. Nothing about bytes. Is there a way to save byte arrays?

Answer

Dave picture Dave · Mar 19, 2014

How about something like this:

IFile file = await FileSystem.Current.GetFileFromPathAsync(fs.filepath);
byte[] buffer = new byte[100];
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(buffer, 0, 100);
}

Edit: Some code taken from http://pclstorage.codeplex.com/

IFile file = await folder.CreateFileAsync("myfile.abc", CreationCollisionOption.ReplaceExisting);

Once you've got the IFile object you should then be able to use this in the same way:

IFile file = await folder.CreateFileAsync("myfile.abc", CreationCollisionOption.ReplaceExisting);
byte[] buffer = new byte[100];
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(buffer, 0, 100);
}