簡體   English   中英

Android OOM For Bitmap with image Capture

[英]Android OOM For Bitmap with image Capture

過去3天我一直在與這種“內存泄漏”進行不間斷的斗爭,雖然我簡化了應用程序的其他部分,但這個問題仍然存在。

首先我想做什么。 我正在使用EXTRA_OUTPUT為IMAGE_CAPTURE觸發一個Intent,以將圖像保存到文件中。 然后,我為用戶顯示按2的順序縮小的返回圖像,讓他選擇圖像的正方形部分。 一旦他選擇了它,他就被帶到另一個Activity,讓他用一些文本等標記圖像,並最終使用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

它在最后一個Activity的setContentView上失敗,因為它向用戶顯示一次圖片,但無論如何都無法再顯示它。

我真的很感激幫助!

更新我一直在多個手機上測試這個問題,唯一一個看起來這樣做的是帶有Froyo的Nexus One。 適用於三星Captivate 2.1,對於Moto Milestone 2.1上的這個問題不會崩潰。

另外,我需要一個500 x 500的圖像,所以我不能縮小2倍以上的因子。

BitmapFactory OOM上看到我的回答讓我瘋狂

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

本機堆是垃圾收集的,但不太頻繁/積極。 我們發現的唯一方法是

  • 監視本機堆(根據上面的鏈接)以確保您沒有遇到OOM異常
  • 當你不需要它們時(通過這樣做來回收原生堆中的空間)釋放位圖(正如你所做的那樣)

    mBitmap.recycle(); mBitmap = null;

您應該嘗試將Bitmap對象包裝在SoftReference對象中,以便在內存不足時以及當您的應用程序未在當前活動中使用該圖像時由操作系統回收它。 此外,您應該考慮在活動更改時放棄圖像占用的內存,並且當您恢復與該特定圖像的活動時,應該再次從文件重新加載圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM