开发者

Monodroid save image from url

Hello the app th开发者_JAVA百科at I'm building works with alot of images that are stored on the server and need to display them on a listview. I would like to be able to store them on a file.

so far here is the code I have

var imageUrl = new Java.Net.URL(obj.imageUrl);
var bitmap = Android.Graphics.BitmapFactory.DecodeStream(imageUrl.OpenStream());
var image = new Android.Graphics.Drawables.BitmapDrawable(bitmap);

but I don't know how to save the image or where to save it.

any help?

thanks


You're overthinking this. :-)

Once you have a Stream:

var imageUrl = new Java.Net.URL(obj.imageUrl);
System.IO.Stream stream = imageUrl.OpenStream();

you can just save it to disk:

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) returns $APPDIR/files, which is Context.FilesDir. You don't necessarily need to use this; Context.CacheDir may be more appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜