0

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

我发现它与我的问题有关,只是它在线加载图像,就我而言,我的应用程序中的所有图像都可以在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上提供的示例

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

4

2 回答 2

2

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

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

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

于 2013-06-11T04:15:08.710 回答
0

最好在需要时下载和显示图像,而不是在可绘制文件夹中拥有如此多的图像。

您应该有效地显示 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 显示图像,请考虑使用延迟加载技术

什么是懒惰列表?

缓存图像并显示

于 2013-06-11T05:34:48.843 回答