gpt4 book ai didi

java - 如何从 URL 检索图像?错误

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

我正在解析 JSON Obj 以获取图像的 url。我正在使用这段代码。

private Drawable LoadImageFromWebOperations(String strPhotoUrl) {
try {
InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
Log.e("TAGG", strPhotoUrl);
return d;
} catch (Exception e) {
Log.e("TAGG", e.toString());
return null;
}
}

但是我收到一个错误,表明这必须在 AsyncTask 中完成,即不同的线程。但是我该如何在 ImageView 中插入可绘制对象呢?由于 DoInBackground() 无法访问 UI 元素。另外,在解析从中获取 URL 的 JSON 对象之前,我无法访问该 URL。那么根据我的情况我可以使用什么解决方案。谢谢!

最佳答案

您可以从 onPostExecute() 访问 UI 元素

因此,您可以在后台线程中进行 API 调用并获取数据。图片下载完成后,您可以在 onPostExecute() 方法中将图片设置到 ImageView 中。

关于java - 如何从 URL 检索图像?错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343119/

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