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 !
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.
Isn't System.IO windows specific?
Nope, why would it be? :)
Okay I found out it isn't, but there's still a problem. Xamarin only has one filepicker. You only need a filepath but that filepicker loads the entire file into memory when picked. I don't know how to get only a file path in Xamarin.