【问题标题】:drawable for bitmap android可绘制位图android
【发布时间】:2013-06-06 20:16:53
【问题描述】:

我的应用程序遇到 OutOfMemory 错误。原因是我的应用程序上加载了很多图像。所以在研究可能的解决方案后,我找到了这个链接并下载了整个示例项目 http://developer.android.com/training/displaying-bitmaps/index.html

我发现它与我的问题有关,只是它在我的情况下在线加载图像,我的应用程序中的所有图像都可以在 drawables..

上找到

所以我的问题是,是否可以使用我的 drawables 中的图像,而不是从特定 URL 加载它。如果是,请给我一个非常简单的样本......

我真的很难做到这一点,因为我是 android 开发新手,而且我只是依赖在线教程.. 谢谢

这就是我要说的......

   public class Images {

/**
 * This are PicasaWeb URLs and could potentially change. Ideally the PicasaWeb API should be
 * used to fetch the URLs.
 *
 * Credit to Romain Guy for the photos:
 * http://www.curious-creature.org/
 * https://plus.google.com/109538161516040592207/about
 * http://www.flickr.com/photos/romainguy
 */
public final static String[] imageUrls = new String[] {
        "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
        "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
        "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
        "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg",
        "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg",
        "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg",
// more …
        "https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s1024/Yosemite%252520Tree.jpg",
};

/**
 * This are PicasaWeb thumbnail URLs and could potentially change. Ideally the PicasaWeb API
 * should be used to fetch the URLs.
 *
 * Credit to Romain Guy for the photos:
 * http://www.curious-creature.org/
 * https://plus.google.com/109538161516040592207/about
 * http://www.flickr.com/photos/romainguy
 */
public final static String[] imageThumbUrls = new String[] {
        "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg",
        "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
        "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg",
        "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",
        "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg",
        "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg",
// … more 
        "https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s160-c/Yosemite%252520Tree.jpg",
};
 }

注意:此代码是此链接上提供的示例http://developer.android.com/training/displaying-bitmaps/index.html

对不起,如果我的解释不够清楚,请随时问我后续问题...谢谢

【问题讨论】:

  • 您应该知道 Intent 内存的大小只有几位,因此您应该处理图像并加载它们的拇指。

标签: android bitmap out-of-memory android-drawable


【解决方案1】:

您不应该在内存中缓存这么多图像。 nostra 有一个很好的库。

图书馆:https://github.com/nostra13/Android-Universal-Image-Loader

尝试实现库。这提供了内存以及硬缓存,不会让您的应用程序因内存而强制关闭。

【讨论】:

    【解决方案2】:

    最好在需要时下载和显示图像,而不是在可绘制文件夹中存放这么多图像。

    您应该有效地显示 btimap。在内存中加载缩小版本

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    还可以在下面的链接中检查管理位图内存

    http://developer.android.com/training/displaying-bitmaps/manage-memory.html.

    如果您遇到内存泄漏,请使用 Mat Analyzer 查找并修复相同问题

    https://www.youtube.com/watch?v=_CruQY55HOk

    如果您从 url 显示图像,请考虑使用延迟加载技术

    What's LazyList?

    Caching images and displaying

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      相关资源
      最近更新 更多