Writing an array of bytes to a storage file on a Windows phone

I am using Visual studio 2012, C #, silverlight, a Windows Phone 8 app.

We get our data from the web service, and through the web service we get the image, which is the base.

I am converting it to an array of bytes, and now I want to save it so that the storage of a Windows phone using a memory stream? I don’t know if it fits right. I don’t want to save it in isolated storage, just in a local folder, because I want to show a picture after a person clicks on a link.

this is what i have so far.

byte[] ImageArray; var image = Attachmentlist.Attachment.ToString(); imagename = Attachmentlist.FileName.ToString(); ImageArray = Convert.FromBase64String(image.ToString()); StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder; await myfolder.CreateFileAsync(imagename.ToString()); StorageFile myfile = await myfolder.GetFileAsync(imagename.ToString()); MemoryStream ms = new MemoryStream(); 

after I initialized the memory stream, how can I take an array of bytes and write it to the storage file, and then return it again?

+6
source share
2 answers

To write the file to disk, try this code:

 StorageFile sampleFile = await myfolder.CreateFileAsync(imagename.ToString(), CreateCollisionOption.ReplaceExisting); await FileIO.WriteBytesAsync(sampleFile, ImageArray); 

A memory stream creates a stream that writes to memory, so it is not applicable to this problem.

+13
source
  StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile imageFile = await folder.CreateFileAsync("Sample.png", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) { using (DataWriter dataWriter = new DataWriter(outputStream)) { dataWriter.WriteBytes(imageBuffer); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } //await outputStream.FlushAsync(); } //await fileStream.FlushAsync(); } 
+6
source

Source: https://habr.com/ru/post/946381/


All Articles