So i have a Xamarin android application which can download files from a server, but images sometimes get heavily distorted (screenshot below), videos can't be played, audio sounds weird etc., but most of the time it gets downloaded properly. And those distortions mostly happen on mobile network, when downloading over wifi, they happen less frequently. Is there something wrong with my download code or maybe the save location has something to do with it?
Code for downloading files:
public static async Task DownloadFileAsync(string url, string mimeType, Stream fileStream) {
long receivedBytes = 0;
long totalBytes = 0;
WebClient client = new WebClient ();
using (var stream = await client.OpenReadTaskAsync (url)) {
byte[] buffer = new byte[1024];
totalBytes = Int64.Parse (client.ResponseHeaders [System.Net.HttpResponseHeader.ContentLength]);
for (;;) {
int bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
await fileStream.WriteAsync (buffer, 0, buffer.Length);
if (bytesRead == 0)
{
await Task.Yield();
break;
}
receivedBytes += bytesRead;
//Raise an event
DownloadProgressMade (receivedBytes, totalBytes);
}
}
}
And the file gets saved like this:
//act = Activity, because downloads are started from a fragment
outFile = act.OpenFileOutput(filename, FileCreationMode.WorldReadable);
await UploadHelper.DownloadFileAsync(url, mimeType, outFile);
Here is a screenshot of a downloaded image:

Update: Not only images get distorted, but other files also get corrupted - videos sometimes can't be played and audio files sound weird.