【发布时间】:2016-01-18 15:18:36
【问题描述】:
在主 Activity 上,我创建了一个在路径列表中循环并下载图像的函数
async void downloadAsync()
{
foreach (string item in string_List)
{
if (!File.Exists(Path))
{
webClient = new WebClient();
var url = new Uri(item);
byte[] imageBytes = null;
imageBytes = await webClient.DownloadDataTaskAsync(url);
//Save the Image using writeAsync
FileStream fs = new FileStream(Path, FileMode.OpenOrCreate);
await fs.WriteAsync(imageBytes, 0, imageBytes.Length);
//Close file connection
fs.Close();
}
}
}
在get view函数的grid view Adapter上放上加载imageview的代码
public override View GetView(int position, View view, ViewGroup parent)
{
// retrieve the view
View vw = view;
ImageView picture;
if (vw == null)
{
// code for create the image view
}
if (File.Exists(Path))
{
Bitmap bitmap = null;
bitmap =BitmapFactory.DecodeFile(path);
picture.SetImageBitmap(bitmap);
}
}
当我在网格上滚动以显示图像时,我发现图像在不正确的位置,直到下载完成,然后一切都在正确的位置调整。
【问题讨论】:
标签: android gridview bitmap xamarin async-await