Search code examples
arraysxamarin.formsisolatedstorage

Xamarin Forms Isolated Storage does not appear to save whole file


I created some code to read and write a byte array (byte[]) to and from isolated storage in my Xamarin Forms app. (Currently just UWP). When I write the file, the byte array should over 7000 bytes. When I read the file from isolated storage, I get 22 bytes and my file (an image file) will of course not display correctly.

Below is my code. Any suggestions would be greatly appreciated.

    private byte[] ReadFromIsolatedStorage(string ps_FileName = "")
    {
        byte[] lobj_ReturnValue = null;

        try
        {
            if (ps_FileName.Trim().Length == 0)
            {
                ps_FileName = "KioskIcon";
            }

            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.FileExists(ps_FileName))
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(ps_FileName, FileMode.Open, isoStore))
                {
                    lobj_ReturnValue = GetImageStreamAsBytes(isoStream);

                }
            }
        }
        catch (Exception ex)
        {
            App.ProcessException(ex);
        }

        return lobj_ReturnValue;
    }

    private byte[] GetImageStreamAsBytes(Stream input)
    {
        var buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }


    private void WriteToIsolatedStorage(byte[] pobj_ByteArray, string ps_FileName = "")
    {
        try
        {
            if (ps_FileName.Trim().Length == 0)
            {
                ps_FileName = "KioskIcon";
            }
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.FileExists(ps_FileName))
            {
                isoStore.DeleteFile(ps_FileName);
            }

            Stream stream = new MemoryStream(pobj_ByteArray);
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(ps_FileName, FileMode.Create, isoStore))
            {
                using (StreamWriter writer = new StreamWriter(isoStream))
                {
                    writer.Write(stream);
                }
            }

        }
        catch (Exception ex)
        {
            App.ProcessException(ex);
        }
    }

Solution

  • OK So I finally got it working. For anyone looking for exact working code, here it is.

    #region IsolatedStorage
        public byte[] ReadImageFile(string ps_AppFolder, string ps_FileName = "")
        {
            byte[] lobj_ReturnValue = null;
            string ls_FullPath = "";
            var lobj_IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                ls_FullPath = (ps_AppFolder + "\\" + (ps_FileName.Trim().Length == 0 ? "KioskIcon" : ps_FileName));
                if (lobj_IsolatedStorage.FileExists(ls_FullPath))
                {
                    var buffer = new byte[16 * 1024];
    
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(ls_FullPath, FileMode.Open, lobj_IsolatedStorage))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            int bytesread;
                            while ((bytesread = isoStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                ms.Write(buffer, 0, bytesread);
                            }
                            return ms.ToArray();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                App.ProcessException(ex);
            }
            return lobj_ReturnValue;
        }
    
        public void SaveImageFile(byte[] pobj_image, string ps_AppFolder,  String ps_FileName = "")
        {
            string ls_FullPath = "";
            try
            {
                var lobj_IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
    
                if (!lobj_IsolatedStorage.DirectoryExists(ps_AppFolder))
                {
                    lobj_IsolatedStorage.CreateDirectory(ps_AppFolder);
                }
    
                ls_FullPath = ps_AppFolder + "\\" + (ps_FileName.Trim().Length == 0 ? "KioskIcon" : ps_FileName);
                if (lobj_IsolatedStorage.FileExists(ls_FullPath))
                {
                    lobj_IsolatedStorage.DeleteFile(ls_FullPath);
                }
    
                MemoryStream ms = new MemoryStream(pobj_image);
    
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(ls_FullPath, System.IO.FileMode.Create, lobj_IsolatedStorage))
                {
                    byte[] buffer = new byte[1024];
                    while (ms.Read(buffer, 0, buffer.Length) > 0)
                    {
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                App.ProcessException(ex);
            }
        }
    
        #endregion