gpt4 book ai didi

image - Monodroid 从 url 保存图像

转载 作者:行者123 更新时间:2023-12-01 10:10:33 25 4
gpt4 key购买 nike

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

到目前为止这是我的代码

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/files,即 Context.FilesDir .您不一定需要使用它; Context.CacheDir可能更合适。

关于image - Monodroid 从 url 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5610860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com