PCLStorage/ZipArchive调试和发布的区别

PCLStorage/ZipArchive Difference between debug and release

大家好,在这个叫做 Whosebug 的伟大城市里的所有聪明人。 我带着我似乎无法解决的问题来找你。

我的问题是我正在 vs2015 中使用 xamarin 表单开发应用程序。 在这个应用程序中,我从网络服务下载了一些压缩数据 (image/audio) 并使用 nuget PCLStorage 将其保存到我的 phone 并(在需要时)将文件解压缩到 display/play.

该应用程序在使用 "Debug" 模式时运行良好,但是当我将应用程序打包为 apk 并将其安装到我的 phone 时,我在尝试打开 zip 时抛出 "Object reference not set to an instance of an object"带 ziarchive 的文件 CheckExistsaAsync 告诉我文件存在,而 await DisplayAlert 告诉我流的长度与文件保存时的长度相同。在调试模式下运行时,它也与文件长度相同。

这里是我失败的代码:

 IFolder rootFolder = FileSystem.Current.LocalStorage;
  if(await rootFolder.CheckExistsAsync("SavedData.zip") == ExistenceCheckResult.FileExists)
  {
    IFile file = await rootFolder.GetFileAsync("SavedData.zip");
    using(Stream s = await file.OpenAsync(FileAccess.Read))
    {
      await DisplayAlert("blabla", s.Length.ToString(), "ok");
      using(ZipArchive zip = new ZipArchive(s, ZipArchiveMode.Read, false))
      {
        //Code
      }
    }
  }

我失败的行是 using(ZipArchive zip = .....) 行抛出异常

这很难说,但我会推测文件权限问题。它在哪里下载文件,Android 应用程序是否具有访问该文件位置的适当权限。

唉!我找到了解决办法!

https://forums.xamarin.com/discussion/45701/system-io-compression-zipfile-extracttodirectory-fails-in-release-mode