问题
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