【问题标题】:Load Image from URL in Android在 Android 中从 URL 加载图像
【发布时间】:2016-11-22 02:49:09
【问题描述】:

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

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

我也尝试了以下代码,但 decodedByte 为空:

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

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

【问题讨论】:

  • 用它来加载 img square.github.io/picasso
  • 感谢您的建议,但不幸的是,毕加索在这种特殊情况下不起作用。我试过了
  • decodedString 的大小是多少?
  • Everytime the image will be saved as a textfile 你没有展示你是如何做到的。而且你没有告诉这个文本文件包含什么。但是 byte[] decodedString 应该包含 png。因此,将 byte[] decodedString 保存到文件以生成 .png 文件。

标签: android image bitmap base64 png


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    滑翔:https://github.com/bumptech/glide

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

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

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

    我强烈建议不要尝试自己处理图像。如果你是一个新手 android 开发者,它可能会变得一团糟。

    【讨论】:

      【解决方案3】:

      您下载的图像非常大((860.000 字节)),无法从中制作位图。所以 BitmapFactory 将返回 null。

      使用前缩小图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        • 2016-06-23
        • 2014-09-10
        • 1970-01-01
        • 2018-07-14
        相关资源
        最近更新 更多