简体   繁体   English

从Android中的URL加载图像

[英]Load Image from URL in Android

I'm trying to load an image from the following url: https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7 我正在尝试从以下URL加载图像: https ://fangkarte.de: 8080/ fangfisch/ file/ files/ 592036af55dfffca0155e01fe10002f7

In Chrome/IE the image will be displayed without problems. 在Chrome / IE中,图像将正确显示。 When I try to load the image on Android I am not able to store the image as an PNG file. 当我尝试在Android上加载图像时,无法将图像存储为PNG文件。 Everytime the image will be saved as a textfile which contains the image as base64 String. 每次图像将被保存为文本文件,其中包含图像为base64字符串。

Also I tried the following code but the decodedByte is null: 我也尝试了以下代码,但codedByte为空:

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Does anyone have an idea how I can show my image from the url as an bitmap or which I prefer to store the image on the device as an png? 有谁知道如何将URL中的图像显示为位图,或者我更喜欢将图像存储为png?

Use Universal Image Loder to upload image from Url Download universal image loder and add to your lib folder and 使用Universal Image Loder从Url上传图像下载Universal Image Loder并添加到您的lib文件夹中,然后

Implement the below code where you want to load image 在要加载图像的位置实现以下代码

ImageLoader imageLoader = ImageLoader.getInstance();

imageLoader.init(ImageLoaderConfiguration.createDefault(context));

DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
        .cacheOnDisc(true).resetViewBeforeLoading(true)
        .showImageForEmptyUri(R.drawable.noimage)
        .showImageOnFail(R.drawable.icon3)
        .showImageOnLoading(R.drawable.loading).build();

imageLoader.displayImage(url, imageview, options);

For handling images, Use Picasso , Glide , Universal image loader, Volley or Fresco 要处理图像,请使用Picasso,Glide,Universal图像加载器,Volley或Fresco

Eg, in picasso, you will be able to load images, store them in memory cache and handle cancellations in listviews/recyclerviews etc... 例如,在毕加索中,您将能够加载图像,将其存储在内存缓存中并处理listviews / recyclerviews等中的取消操作...

Picasso.with(context).load(" https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7 ").placeholder("someimagetillyourimagedownloads).into(R.id.yourimageview); Picasso.with(context).load(“ https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7 ”).placeholder(“ someimagetillyourimagedownloads).into(R.id.yourimageview);

Picasso: https://github.com/square/picasso 毕加索: https : //github.com/square/picasso

Glide: https://github.com/bumptech/glide 滑行: https : //github.com/bumptech/glide

Fresco: https://github.com/facebook/fresco ( I use this after extensively testing all 3 except volley ) 壁画: https : //github.com/facebook/fresco (我在广泛测试了除凌空以外的所有3个之后使用了它)

UIL: https://github.com/nostra13/Android-Universal-Image-Loader UIL: https//github.com/nostra13/Android-Universal-Image-Loader

Similar things can be done using all the above libraries. 使用上述所有库都可以完成类似的操作。

I highly recommend not trying to handle images on your own. 我强烈建议不要尝试自行处理图像。 It can turn into quite a mess if you are a novice android dev. 如果您是新手android开发人员,可能会变得一团糟。

The image you download is much to big ((860.000 bytes)) to make a Bitmap out of it. 您下载的图像很大((860.000字节)),可以从中制作出位图。 So BitmapFactory will return null. 因此,BitmapFactory将返回null。

Scale image down before use. 使用前按比例缩小图像。

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

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