C#-下载被破坏
因此,我有一个Xamarin android应用程序,它可以从服务器下载文件,但图像有时会严重失真(下面的屏幕截图),视频无法播放,音频听起来很奇怪,等等,但大多数时候它都能正常下载。这些失真大多发生在移动网络上,当通过wifi下载时,发生的频率较低。我的下载代码是否有问题,或者可能与保存位置有关 下载文件的代码:C#-下载被破坏,c#,android,xamarin,download,C#,Android,Xamarin,Download,因此,我有一个Xamarin android应用程序,它可以从服务器下载文件,但图像有时会严重失真(下面的屏幕截图),视频无法播放,音频听起来很奇怪,等等,但大多数时候它都能正常下载。这些失真大多发生在移动网络上,当通过wifi下载时,发生的频率较低。我的下载代码是否有问题,或者可能与保存位置有关 下载文件的代码: public static async Task DownloadFileAsync(string url, string mimeType, Stream fileStream)
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);
}
}
}
文件将按如下方式保存:
//act = Activity, because downloads are started from a fragment
outFile = act.OpenFileOutput(filename, FileCreationMode.WorldReadable);
await UploadHelper.DownloadFileAsync(url, mimeType, outFile);
以下是下载图像的屏幕截图:
更新:不仅图像会失真,其他文件也会损坏-视频有时无法播放,音频文件听起来很奇怪