Xamarin 窗体独立存储似乎不会保存整个文件



我创建了一些代码来在我的 Xamarin 窗体应用中向独立存储读取和写入字节数组 (byte[](。 当我写入文件时,字节数组应该超过 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

最新更新