Monodroid从url保存图像
问题描述:
你好我正在建设的应用程序与存储在服务器上的许多图像一起工作,需要在列表视图中显示它们。我希望能够将它们存储在一个文件中。Monodroid从url保存图像
到目前为止,这里是我的代码有
var imageUrl = new Java.Net.URL(obj.imageUrl);
var bitmap = Android.Graphics.BitmapFactory.DecodeStream(imageUrl.OpenStream());
var image = new Android.Graphics.Drawables.BitmapDrawable(bitmap);
,但我不知道如何保存图像或保存位置。
有帮助吗?
谢谢
答
你正在推翻这一点。 :-)
一旦你有一个Stream
:
var imageUrl = new Java.Net.URL(obj.imageUrl);
System.IO.Stream stream = imageUrl.OpenStream();
你可以将它保存到磁盘:
using (var o = File.Open(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"file-name"))) {
byte[] buf = new byte[1024];
int r;
while ((r = stream.Read(buf, 0, buf.Length)) > 0)
o.Write (buf, 0, r);
}
Environment.GetFolderPath(Environment.SpecialFolder.Personal)
收益$ APPDIR /文件,这是Context.FilesDir。你不一定需要使用它; Context.CacheDir可能更合适。
谢谢jonp。我认为缓存将是最好的,但只是为了确保这是我应该使用的路径? System.Environment.SpecialFolder.InternetCache – adam 2011-04-14 06:04:30
没关系jonp ** this.ApplicationContext.CacheDir.ToString(); **弄明白了。但嘿非常感谢你的代码我真的很感激它! – adam 2011-04-14 07:40:37