Xamarin forms - Open file as stream

青春壹個敷衍的年華 提交于 2019-12-11 00:15:37

问题


This might be a very simple question but I'm always confused when we're talking about streams. I'm trying to open a file in my Xamarin Forms project in the Android part of it. I have a Java.IO.File which I would like to convert into a stream to be able to send it to an Azure Blob Storage with that simple function :

        public async Task<string> UploadFileAsync(Stream stream)
        {
        await this.container.CreateIfNotExistsAsync();

        var name = "Photo_" + Guid.NewGuid().ToString();
        var fileBlob = container.GetBlockBlobReference(name);
        await fileBlob.UploadFromStreamAsync(stream);

        return name;
        }

However, I can't seem to be able to use StreamReader from the Android part with StreamReader or OutputStreamWriter. Did anyone encounter the same issue ? Thank you !


回答1:


You should be able to just use the StreamReader class from System.IO. Sample code could look something like this:

FileStream fs = new FileStream("photo.jpg", FileMode.Open, FileAccess.Read);

StreamReader r = new StreamReader(fs);

Don't forget to dispose them after you are done.

For more detail on how to upload to Azure containers, check out the Xamarin documentation.



来源:https://stackoverflow.com/questions/43341155/xamarin-forms-open-file-as-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!