Warm tip: This article is reproduced from serverfault.com, please click

Xamarin forms

发布于 2017-04-11 08:56:33

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 !

Questioner
Pierre P.
Viewed
0
661 2019-10-11 14:33:53

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.