简体   繁体   English

将图片从网址加载到图片视图

[英]Load a Image from an Url to an Image View

I am developing an Android app for which I am working with an API. 我正在开发要使用API​​的Android应用。

The API is returning certain results such as an URL of an Image. API返回某些结果,例如图像的URL。

Since I have always been using Glide library to load offline and online images to an ImageView so for this also I am using the Glide Library. 由于我一直使用Glide库将脱机和在线图像加载到ImageView中,因此我也使用了Glide库。

But the problem this time is that sometimes the url which is returned through an API is not returning an Image file but is returning a normmal file only. 但是这次的问题是,有时通过API返回的url不会返回Image文件,而仅返回标准文件。 I mean that if I paste that url in an web browser then the file which is being downloaded is not in an Image form but just in an file form. 我的意思是,如果我将该URL粘贴到Web浏览器中,则正在下载的文件不是图像形式,而只是文件形式。 Hence when I try to use that url with the Glide library it is not working. 因此,当我尝试在Glide库中使用该url时,它将无法正常工作。

But after downloading if I change the extension of the file to an image extension then the file is being opened properly as an image. 但是下载后,如果我将文件的扩展名更改为图像扩展名,则该文件将作为图像正确打开。

Any help on how a url which is returning a general file can be used to show an image would be really helpful. 关于如何使用返回一般文件的url来显示图像的任何帮助将非常有帮助。

This is how I am using the Glide Library 这就是我使用滑翔库的方式

Glide.with(context.getApplicationContext())
                .load(GetImageUrl())
                .asBitmap()
                .listener(new RequestListener<String, Bitmap>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {

                        return false;
                    }
                })
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .thumbnail(0.5f)
                .into(image);

Just rename your file with added extension and load it using glide. 只需使用添加的扩展名重命名文件并使用glide加载它即可。 It will works the same. 它将起作用。 Like if you are getting a file namely 123, before loading that image using glide just add 123 + ".jpg" or whatever extension is and load. 就像您要获取的文件名为123一样,在使用glide加载该图像之前,只需添加123 +“ .jpg”或任何扩展名即可加载。 It's that simple. 就这么简单。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM