Xamarin Forms Isolated Storage 似乎没有保存整个文件

Xamarin Forms Isolated Storage does not appear to save whole file

我创建了一些代码来在我的 Xamarin Forms 应用程序中读取和写入字节数组 (byte[]) 和独立存储。 (目前只有 UWP)。当我写文件时,字节数组应该超过 7000 字节。当我从隔离存储读取文件时,我得到 22 个字节,我的文件(图像文件)当然无法正确显示。

下面是我的代码。任何建议将不胜感激。

    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);
        }
    }

好的所以我终于让它工作了。对于寻找准确工作代码的任何人,这里是。

#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