0

我一直在這個「內存泄漏」過去3天不間斷,而我簡化了我的應用程序的其他部分,這個問題一直存在。Android的OOM位圖與圖像捕獲

首先我正在嘗試做什麼。我正在用EXTRA_OUTPUT激發IMAGE_CAPTURE的意圖以將圖像保存到文件中。然後,我顯示縮小了2倍的返回圖像,供用戶選擇圖像的正方形部分。一旦他選擇了它,他就會被帶到另一個活動,讓他用一些文本等標記圖像,並最終使用HTTPPost將所有內容上傳到服務器。

現在,一切正常工作一次!我可以通過它,一切正常,沒有任何問題。如果我嘗試拍攝另一張照片,我總是會遇到內存不足異常。這裏是logcat的爲它的發生

02-18 19:07:19.498: ERROR/dalvikvm-heap(6385): 1040400-byte external allocation too large for this process. 
02-18 19:07:19.498: ERROR/GraphicsJNI(6385): VM won't let us allocate 1040400 bytes 
02-18 19:07:19.498: DEBUG/AndroidRuntime(6385): Shutting down VM 
02-18 19:07:19.498: WARN/dalvikvm(6385): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): FATAL EXCEPTION: main 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hitpost/com.hitpost.SharePicture}: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown> 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.os.Looper.loop(Looper.java:123) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at dalvik.system.NativeStart.main(Native Method) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown> 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.createView(LayoutInflater.java:513) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.Activity.setContentView(Activity.java:1647) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at com.hitpost.SharePicture.onCreate(SharePicture.java:90) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  ... 11 more 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: java.lang.reflect.InvocationTargetException 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.widget.ImageView.<init>(ImageView.java:108) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at java.lang.reflect.Constructor.constructNative(Native Method) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.view.LayoutInflater.createView(LayoutInflater.java:500) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  ... 23 more 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.Bitmap.nativeCreate(Native Method) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.content.res.Resources.loadDrawable(Resources.java:1709) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  at android.widget.ImageView.<init>(ImageView.java:118) 
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):  ... 27 more 

它是失敗的最後一項活動的的setContentView,在它顯示的畫面就會向用戶的最晚時間,但不知何故無法再次顯示。

我真的很感謝幫助!

更新 我已經在多部電話測試這個問題,而且是唯一的地方似乎是在做,這是Nexus One的升級Froyo用。在三星Captivate 2.1上工作,不會在Moto Milestone 2.1上遇到這個問題。

而且我需要在最後一個500×500的圖像,所以我不能超過按比例縮小的2倍。

+0

您可能想要進一步縮小圖像。根據縱橫比和圖像的原始尺寸計算縮放比例。 – Samuh 2011-02-19 04:52:29

+0

Samuh,但是,由於我需要一張500x500的圖片上傳到服務器,因此我無法縮小比例。我將更新描述與更多細節。 – Leo 2011-02-21 21:32:39

回答

2

看到我的回覆在BitmapFactory OOM driving me nuts

您的位圖數據位於Native堆中,而不是VM堆。因此顯式的虛擬機垃圾收集將不起作用。

本地堆是垃圾收集但不太頻繁/積極。我們已經找到了工作的唯一方法是

  • 監視本機堆(按照上面的鏈接),以確保你不打的OOM異常
  • 騰出位圖時,你不需要他們(這將回收原生堆中的空間)通過做(如您所做)

    mBitmap.recycle(); mBitmap = null;

0

你應該嘗試在SoftReference對象包裹位圖對象,以便它能夠回收操作系統在內存不足的情況下以及應用程序未在當前活動中使用映像的情況。當你的活動改變時,你也應該考慮放棄圖像佔用的內存,並且當你重新開始特定圖像的活動時,應該重新從文件中重新加載圖像。