使用 Glide 从 URL 加载图像但没有扩展 [英] Use Glide to load images from URL but without extensions

查看:74
本文介绍了使用 Glide 从 URL 加载图像但没有扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 Glide 通过以下代码从我的本地 Web 服务器加载图像:

I could use Glide to load images from my local web server via the following code:

Glide.with(this)
     .load(SERVER_HOST_ADDRESS + userID + ".jpg")
     .into((ImageView) mBinding.getRoot().findViewById(R.id.userImage));

问题在于,使用这种方法时,我只能访问.jpg 扩展名,有时用户可能选择上传不同类型的图像.有没有办法让 Glide 处理从 URL 加载图像,但没有扩展?构建迭代器没问题,但可能没有必要.

The problem is that with this method, I am limited to having access only to.jpg extensions, when sometimes the user may have chosen to upload a different type of image. Is there a way for Glide to handle loading an image from a URL, but without the extensions? Building an iterator would be fine, but may be unnecessary.

推荐答案

有没有办法让 Glide 处理从 URL 加载图像,但没有扩展?

Is there a way for Glide to handle loading an image from a URL, but without the extensions?

是的,尽管您的 Web 服务器可能不支持它.

Yes, though your Web server may not support it.

我的解释是你想要:

.load(SERVER_HOST_ADDRESS + userID)

如果您的 Web 服务器在该 URL 处提供图像,并且具有有效的图像 MIME 类型,Glide 将处理它.Glide 本身并不关心文件扩展名 —它需要一个提供有效图像 MIME 类型的有效 URL.

If your Web server serves an image at that URL, with a valid image MIME type, Glide will handle it. Glide itself does not care about file extensions — it wants a valid URL that supplies a valid image MIME type.

但是,我猜您的服务器会为该 URL 返回 404 错误,因为服务器期望文件扩展名与服务器上的文件匹配.

However, my guess it that your server will return a 404 error for that URL, because the server is expecting the file extension, to match up with the file on the server.

处理此问题的选项包括:

Options to deal with this include:

  • 教服务器如何处理无扩展名的 URL

  • Teach the server how to handle the no-extension URL

使用一致的图像类型(例如,让服务器将所有内容都转换为 JPEG)

Use a consistent image type (e.g., have the server convert everything to JPEG)

让一些元数据告诉您要加载的 URL(例如,用于获取用户详细信息的 Web 服务调用具有提供图像 URL 的 JSON 属性)

Have some metadata tell you what URL to load (e.g., a Web service call to get details of the user has a JSON property that provides the image URL)

按照你的建议去做,只是迭代各种可能性,看看它们中的一个是否有效

Do what you suggested and just iterate over the various possibilities and see if one of them works

这篇关于使用 Glide 从 URL 加载图像但没有扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆