我正在调用 GET api,它返回一个图像/jpeg contentType 字符串,我正在使用 urlConnection 来处理请求。 如何在 ImageView 中显示响应?
GET https://httpbin.org/image/jpeg
它返回奇怪的字符串。
我试图将响应保存到文件并显示它,但没有成功。
您可以使用第三方库从
URL
(Glide、Landscapist、Coil)加载和显示图像。它们易于使用并支持开箱即用的缓存图像。
如果您正在使用 jetpack compose,我建议您使用 Landscapist,因为它在 compose 中具有更多功能。如果不是 Glide 和 Coil 也可以。
加载图片的例子:
滑行:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("https://www.example.com/image.jpg").into(imageView);
线圈:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.load("https://www.example.com/image.jpg");
风景画家:
// with glide
GlideImage(
imageModel = { "https://www.example.com/image.jpg" }, // loading a network image using an URL.
imageOptions = ImageOptions(
contentScale = ContentScale.Crop,
alignment = Alignment.Center
)
)
// with coil
CoilImage(
imageModel = { "https://www.example.com/image.jpg" }, // loading a network image or local resource using an URL.
imageOptions = ImageOptions(
contentScale = ContentScale.Crop,
alignment = Alignment.Center
)
)
// with fresco
FrescoImage(
imageUrl = "https://www.example.com/image.jpg", // loading a network image using an URL.
imageOptions = ImageOptions(
contentScale = ContentScale.Crop,
alignment = Alignment.Center
)
)