我收到OOM错误,我需要使用整个位图来运行算法。
错误:
Process: edu.illinois.ece.nano.labaware, PID: 10349 java.lang.OutOfMemoryError: Failed to allocate a 357739212 byte allocation with 3485344 free bytes and 251MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
at android.content.res.Resources.getDrawable(Resources.java:767)
at android.content.res.Resources.getDrawable(Resources.java:738)
at edu.illinois.ece.nano.labaware.ViewImageActivity.onCreate(ViewImageActivity.java:69)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
50: Log.d(TAG,"Going to convert image.");
69: Drawable drawable = getResources().getDrawable(R.drawable.t);
答案 0 :(得分:0)
350 + MB太大而无法在大多数移动设备上显示。请注意,即使您将窗口小部件(例如ImageView)的宽度和高度设置为较小的尺寸以显示此图像,图像仍会在窗口小部件上显示之前完全加载到内存中。
您可以按照this guideline将位图子采样为您需要在屏幕上显示的大小,这样可以大大减少内存使用量。如果要求此图像完全显示在可滚动区域(例如大图)中,您可以: