簡體   English   中英

Xamarin Forms 隔離存儲似乎無法保存整個文件

[英]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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM