流.在AssetManager.Open上不支持Seek ?



我目前正尝试着将基于windows的游戏移植到Android上,但是我却无法将文件系统加载到手机上。

我将我的文件以归档格式存储,以避免不得不处理不同的文件系统,而只是创建我自己的文件系统。但是,它依赖于FileStreams能够查找归档文件中数据的正确位置。

根据微软的文档,Access。随机存取。流应该支持流内搜索,但无论我做什么,使用AssetManager创建的流。当我调用Stream时,Open(string, Access)崩溃。从它查找,给出一个NotSupportedException。

这是我的错误,还是SDK上的已知缺陷,是否有解决方案?

提前感谢。

编辑:按要求,一个最小可复制的示例。

//class Game is a part of the MonoGame SDK. Game.Activity.Assets is a direct, unmodified version of
//Android.Content.Res.AssetManager, at least according to the docs.
//I believe the Seek function not being supported is a part of Xamarin's SDK, not Monogame's.
Stream stream = Game.Activity.Assets.Open(PathToArchive, Android.Content.Res.Access.Random);
stream.Seek(0, SeekOrigin.Begin); //causes exception regardless of seek value provided, and regardless of where the function is called.

是的,来自Assets.Open()的流确实不支持Seek操作。你可以通过CanSeek的性质来验证这一点。它会给你false

你可以这样做:

Stream stream = Assets.Open(PathToArchive, Android.Content.Res.Access.Random);
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);//use ms in place of stream 

最新更新