12

È questo l'unico/corretto modo di ottenere un flusso da un file di risorse?Ottenere uno stream da un file/contenuto di risorse

Uri uri = new Uri(fullPath); 

    StorageFile storageFile = 
     await Windows.Storage.StorageFile. 
     GetFileFromApplicationUriAsync(uri); 

    IRandomAccessStreamWithContentType randomAccessStream = 
     await storageFile.OpenReadAsync(); 

    IInputStream resourceStream = (IInputStream) 
     randomAccessStream.GetInputStreamAt(0); 

Tutte le mie altre fonti (http e storage locale) restituisce un oggetto Stream, ed è doloroso dover if-else codice che utilizza uno o l'altro.

Ho anche provato a creare un MemoryStream, ma non riesco nemmeno a trovare un modo per ottenere i byte ... Si prega di aiutare.

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() durante la lettura da risorsa: http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.iinputstream.readasync.aspx

mentre Stream.Read() e Stream.ReadAsync() simile a questa:

http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

e

http://msdn.microsoft.com/en-us/library/hh137813.aspx

Grazie

risposta

23

Ok ho trovato!

StorageFile storageFile = 
     await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri); 

    var randomAccessStream = await storageFile.OpenReadAsync(); 
    Stream stream = randomAccessStream.AsStreamForRead(); 
+0

è possibile convertire il flusso di StorageFile? : o – jdnichollsc

7

Si può anche farlo in una linea meno:

Stream stream = await storageFile.OpenStreamForReadAsync(); 
Problemi correlati