我目前正在使用Visual Studio 2012 IDE,我正在使用C#和Mono for Android框架开发Android应用程序。
我的问题是你如何正确地请求你的应用程序下载一堆文件并将它们存储在你的设备上?
目前,我无法下载所需的69 xml文件(总计大约6 MB),因为它返回“* ABORTING:HEAP MEMORY CORRUPTION in tmalloc_large *”错误。但是,我测试了30个文件(大约3 MB)并且下载它们就好了,我可以使用它们。我需要所有69个文件来运行我的应用程序。请帮忙。以下是我正在使用的方法,请提供更正,添加或替代方法。我非常感激。
public void DownloadLanguagePack(string language, string[] files)
{
// Download each file inside the 'files' array
for (var i = 0; i < files.Length; i++)
{
string[] args = { language, files[i].FileName};
// This way...
WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(new System.Uri(files[i].FileURL), args);
}
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string[] args = (string[])e.UserState;
string language = args[0];
string filename = args[1] + ".xml";
if (Android.OS.Environment.ExternalStorageState == Android.OS.Environment.MediaMounted)
{
// Directory logic, works just fine
var dir = new Java.IO.File(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal));
var folder = new Java.IO.File(dir, language);
var file = new Java.IO.File(folder, filename);
if (!folder.Exists())
{
folder.Mkdirs();
}
// Tested with & without ThreadPool and makes no difference.
ThreadPool.QueueUserWorkItem((c) =>
{
// THIS IS THE AREA THAT I BELIEVE NEEDS SOME WORK, MAYBE NOT.
XDocument doc = new XDocument();
using (Stream s = e.Result)
{
// Load xml into document
doc = XDocument.Load(s);
using (var stream = new StreamWriter(file.Path, false))
{
// Save document
doc.Save(stream);
stream.Flush();
stream.Close();
GC.Collect();
}
}
});
}
}