简体   繁体   English

调用getDrawable()时java.lang.OutOfMemoryError

[英]java.lang.OutOfMemoryError when calling getDrawable()

I get the following out of memory error and I'm not sure why. 我收到以下内存不足错误,我不确定为什么。

Failed to allocate a 1860508 byte allocation with 905740 free bytes and 884KB until OOM

java.lang.OutOfMemoryError: Failed to allocate a 1860508 byte allocation with 905740 free bytes and 884KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656) 
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037) 

at android.content.res.Resources.loadDrawableForCookie(Resources.java:4056) 
at android.content.res.Resources.loadDrawable(Resources.java:3929) 
at android.content.res.Resources.getDrawable(Resources.java:1991) 
at android.content.res.Resources.getDrawable(Resources.java:1973) 
at android.graphics.drawable.LevelListDrawable.inflate(LevelListDrawable.java:128) 
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1192) 
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1086) 
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4045) 
at android.content.res.Resources.loadDrawable(Resources.java:3929) 
at android.content.res.Resources.getDrawable(Resources.java:1991) 
at android.content.res.Resources.getDrawable(Resources.java:1973) 
at android.content.Context.getDrawable(Context.java:409)
at com.MyCompany.MyApp.Login.updateWifiStatus(Login.java:942)
at com.MyCompany.MyApp.Login$6.run(Login.java:576) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6939) 
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

I am using a 5 level resource to indicate wifi status and here is the code I'm using 我正在使用5级资源来指示wifi状态,这是我正在使用的代码

    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    // Level of current connection
    int rssi = wifiManager.getConnectionInfo().getRssi();
    int level = WifiManager.calculateSignalLevel(rssi, 5);
    Drawable db = getDrawable(R.drawable.stat_sys_wifi_signal);
    db.setLevel(level);
    wifiMenu.setIcon(db);

The images are all the same size (512x512). 图像大小均相同(512x512)。

In reference to other answers out there I have got largeheap in my manifest. 关于那里的其他答案,我的清单上有很多东西。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Use TinyPNG 使用TinyPNG

TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. TinyPNG使用智能有损压缩技术来减小PNG文件的文件大小。 By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. 通过有选择地减少图像中的颜色数量,需要较少的字节来存储数据。 The effect is nearly invisible but it makes a very large difference in file size! 效果几乎是看不见的,但文件大小却有很大差异!

Compress all images using this link. 使用此链接压缩所有图像。

Reduce the size of all picture before use in your Android applications. 在Android应用程序中使用之前,请减小所有图片的尺寸。

Make sure you read the document Loading Large Bitmaps Efficiently given by @mudit_sen. 确保阅读了@mudit_sen给出的有效加载大型位图文档。

Hope this may help you. 希望这对您有帮助。

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

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