尝试加载1 MB图像时Android抛出内存不足错误 [英] Android throwing out of memory error when trying to load 1 megabyte image

查看:100
本文介绍了尝试加载1 MB图像时Android抛出内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关代码:

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ImageButton btn = (ImageButton) findViewById(R.id.info_image);
            btn.setImageResource(currentID[currentIDPos]);
        }
    });

这就是说run方法正在分配400兆字节的内存.我的jpg大小不超过一兆字节.计时器每3-4秒左右单击一次就可以在计时器上调用run()方法,但这没关系,因为从我可以使用printlns得知,在崩溃之前,run()仅被调用一次.

It's saying that the run method is making a 400-some megabyte allocation. My jpgs are no more than a megabyte in size. The run() method is called on a timer every 3-4 seconds or so and on click, but that shouldn't matter because from what I can tell using printlns the run() is only called once before the crash.

这是例外.必须再次运行它才能获得此功能,这次的内存量不同,但仍然很大.

Here's the exception. Had to run it again to get this, it's a different amount of memory this time but still very large.

FATAL EXCEPTION: main
                                                                Process: com.grey.handsaver, PID: 32531
                                                                java.lang.OutOfMemoryError: Failed to allocate a 215151564 byte allocation with 33554400 free bytes and 100MB until OOM
                                                                    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
                                                                    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2580)
                                                                    at android.content.res.Resources.loadDrawable(Resources.java:2487)
                                                                    at android.content.res.Resources.getDrawable(Resources.java:814)
                                                                    at android.content.res.XResources.getDrawable(XResources.java:572)
                                                                    at android.content.Context.getDrawable(Context.java:403)
                                                                    at android.widget.ImageView.resolveUri(ImageView.java:747)
                                                                    at android.widget.ImageView.setImageResource(ImageView.java:398)
                                                                    at com.grey.handsaver.ExerciseInfoActivity$3.run(ExerciseInfoActivity.java:128)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5430)
                                                                    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:913)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
                                                                    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)

推荐答案

好的,我知道了.我使用的是lg g4,它使用xxxhdpi,并且我的图像位于可绘制文件夹中,而不是可绘制xxxhdpi文件夹中.这无需使用毕加索或其他方法即可解决我的问题.

Alright, I figured it out. I'm using an lg g4, which uses xxxhdpi, and my images were in the drawable folder, not the drawable-xxxhdpi folder. This solved my problem without using picasso or the other methods.

这篇关于尝试加载1 MB图像时Android抛出内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆