这是从资源文件中获取流的正确/唯一方法吗?
Uri uri = new Uri(fullPath);
StorageFile storageFile =
await Windows.Storage.StorageFile.
GetFileFromApplicationUriAsync(uri);
IRandomAccessStreamWithContentType randomAccessStream =
await storageFile.OpenReadAsync();
IInputStream resourceStream = (IInputStream)
randomAccessStream.GetInputStreamAt(0);
我的所有其他源(http和本地存储)都返回一个Stream对象,并且使用if-else代码使用其中一个或其他代码是很痛苦的。
我也尝试过创建一个MemoryStream,但是我甚至找不到解决这些字节的方法......请帮忙。
uint size = (uint)randomAccessStream.Size;
IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
await randomAccessStream.ReadAsync(buffer, size,
InputStreamOptions.None);
Stream stream = new MemoryStream(buffer); // error takes byte[] not IBuffer
从资源中读取时,IInputStream.ReadAsync(): http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.iinputstream.readasync.aspx
而Stream.Read()和Stream.ReadAsync()看起来像这样:
http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
和
http://msdn.microsoft.com/en-us/library/hh137813.aspx
由于
答案 0 :(得分:24)
好的,我找到了它!
StorageFile storageFile =
await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
var randomAccessStream = await storageFile.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();
答案 1 :(得分:7)
您也可以在少一行中完成:
Stream stream = await storageFile.OpenStreamForReadAsync();