2017-03-08 133 views
-1

我得到一個OOM錯誤,我需要使用整個位圖才能運行我的算法。Android位圖內存不足錯誤

錯誤:

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

真的嗎?您需要將340 + MB圖形加載到內存中才能使算法正常工作?嘗試重新設計你的算法。 –

+0

你有沒有嘗試調整你的圖片大小來獲取這個異常? – MuTiny

+0

我們在這裏需要更多的上下文。你會遇到內存不足的錯誤,因爲你正在爲內存加載巨大的圖像。這似乎相當期待。你有什麼需要幫助的?你是否獨立分析圖像的碎片?你是否試圖分析圖像中不同對象之間的關係?你在考慮什麼替代方法? – Tyzoid

回答

0

350 + MB僅僅是方式太大而不能顯示在大多數移動設備。請注意,即使您將小部件(例如ImageView)的寬度和高度設置爲較小的尺寸以顯示此圖像,圖像仍然會在顯示在小部件上之前完全加載到內存中。

您可以按照this guideline的要求對位圖進行二次採樣,使其成爲您需要在屏幕上顯示的大小,這樣做可以大幅降低內存使用量。如果需要該圖像被完全在可滾動區域(例如大圖)顯示,可以:

  1. 使用BitmapRegionDecoder從該圖像滾動時加載特定的區域,或
  2. 大圖像分割成多個較小的圖像,並創建一個html文件,通過組合所有這些較小的圖像來顯示大圖,並將這個文件和圖像放置在assets文件夾中,並使用WebView加載html文件。