在Android中从URL加载图片

时间:2016-07-19 09:32:13

标签: android image bitmap base64 png

我尝试从以下网址加载图片: https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7

在Chrome / IE中,图像会毫无问题地显示出来。当我尝试在Android上加载图像时,我无法将图像存储为PNG文件。每次将图像保存为文本文件,其中包含图像为base64 String。

此外,我尝试了以下代码,但decodeByte为null:

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

有没有人知道如何将我的图像从网址显示为位图,或者我更喜欢将图像作为png存储在设备上?

3 个答案:

答案 0 :(得分:0)

使用Universal Image Loder从Url上传图片 下载通用图片加载器并添加到您的lib文件夹和

在您要加载图片的位置实现以下代码

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

答案 1 :(得分:0)

用于处理图像,使用Picasso,Glide,通用图像加载器,Volley或Fresco

例如,在毕加索中,您将能够加载图像,将它们存储在内存缓存中并处理listviews / recyclerviews等中的取消...

Picasso.with(上下文).load( “https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7”)占位符(“someimagetillyourimagedownloads).into(R.id.yourimageview);

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

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

Fresco:https://github.com/facebook/fresco(我在广泛测试所有3个除了凌空之后使用它)

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

使用上述所有库都可以完成类似的事情。

我强烈建议您不要自己处理图像。如果你是一个新手android开发者,它会变得相当混乱。

答案 2 :(得分:0)

您下载的图像要大得多((860.000字节))以使其成为一个Bitmap。所以BitmapFactory将返回null。

使用前缩小图像。