Jer Yango
Jer Yango

Reputation: 592

Use Glide to load images from URL but without extensions

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));

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.

Upvotes: 1

Views: 2768

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

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

Yes, though your Web server may not support it.

My interpretation is that you want:

.load(SERVER_HOST_ADDRESS + userID)

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.

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:

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

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

  • 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

Upvotes: 1

Related Questions