Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image Monodroid从url保存图像_Image_Url_Xamarin.android - Fatal编程技术网

Image Monodroid从url保存图像

Image Monodroid从url保存图像,image,url,xamarin.android,Image,Url,Xamarin.android,您好,我正在构建的应用程序使用了大量存储在服务器上的图像,需要在列表视图中显示它们。我希望能够将它们存储在一个文件中 到目前为止,这是我的代码 var imageUrl = new Java.Net.URL(obj.imageUrl); var bitmap = Android.Graphics.BitmapFactory.DecodeStream(imageUrl.OpenStream()); var image = new Android.Graphics.Drawables.BitmapD

您好,我正在构建的应用程序使用了大量存储在服务器上的图像,需要在列表视图中显示它们。我希望能够将它们存储在一个文件中

到目前为止,这是我的代码

var imageUrl = new Java.Net.URL(obj.imageUrl);
var bitmap = Android.Graphics.BitmapFactory.DecodeStream(imageUrl.OpenStream());
var image = new Android.Graphics.Drawables.BitmapDrawable(bitmap);
但我不知道如何保存图像,也不知道保存在哪里

有什么帮助吗


谢谢你想得太多了。:-)

一旦有了

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/files,即。你不一定要用这个;可能更合适。

谢谢jonp。我认为缓存是最好的,但只是为了确保这是我应该使用的路径?System.Environment.SpecialFolder.InternetCache别管jonp this.ApplicationContext.CacheDir.ToString();我明白了。但是,嘿,非常感谢你的代码,我真的很感激!