2014-01-10 77 views
0

我的背景圖片太大,所以,出現以下錯誤的應用程序崩潰(我從logcat的抄襲其他有用的信息,以及)太大圖片:內存不足異常

01-10 14:53:48.799: E/dalvikvm-heap(9297): Out of memory on a 6955024-byte allocation. 
01-10 14:53:48.799: I/dalvikvm(9297): "main" prio=5 tid=1 RUNNABLE 
01-10 14:53:48.799: I/dalvikvm(9297): | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8 
01-10 14:53:48.799: I/dalvikvm(9297): | sysTid=9297 nice=0 sched=0/0 cgrp=apps handle=1073849308 
01-10 14:53:48.812: I/dalvikvm(9297): | state=R schedstat=(38497557466 45560972992 4658) utm=3335 stm=514 core=0 
01-10 14:53:48.840: I/dalvikvm(9297): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.content.res.Resources.loadDrawable(Resources.java:1965) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
01-10 14:53:48.840: I/dalvikvm(9297): at android.view.View.<init>(View.java:3330) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.View.<init>(View.java:3259) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.ViewGroup.<init>(ViewGroup.java:425) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.widget.RelativeLayout.<init>(RelativeLayout.java:210) 
01-10 14:53:48.860: I/dalvikvm(9297): at java.lang.reflect.Constructor.constructNative(Native Method) 
01-10 14:53:48.860: I/dalvikvm(9297): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
01-10 14:53:48.860: I/dalvikvm(9297): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-10 14:53:48.860: I/dalvikvm(9297): at com.xxx.xxx.InputFragment.onCreateView(InputFragment.java:70) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.Activity.performStart(Activity.java:5114) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.ActivityThread.access$700(ActivityThread.java:141) 
01-10 14:53:48.860: I/dalvikvm(9297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
01-10 14:53:48.910: I/dalvikvm(9297): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-10 14:53:48.910: I/dalvikvm(9297): at android.os.Looper.loop(Looper.java:137) 
01-10 14:53:48.920: I/dalvikvm(9297): at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-10 14:53:48.920: I/dalvikvm(9297): at java.lang.reflect.Method.invokeNative(Native Method) 
01-10 14:53:48.920: I/dalvikvm(9297): at java.lang.reflect.Method.invoke(Method.java:511) 
01-10 14:53:48.920: I/dalvikvm(9297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-10 14:53:48.920: I/dalvikvm(9297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-10 14:53:48.920: I/dalvikvm(9297): at dalvik.system.NativeStart.main(Native Method) 
01-10 14:53:49.315: D/skia(9297): --- decoder->decode returned false 
01-10 14:53:49.315: D/AndroidRuntime(9297): Shutting down VM 
01-10 14:53:49.320: W/dalvikvm(9297): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
01-10 14:53:49.570: E/AndroidRuntime(9297): FATAL EXCEPTION: main 
01-10 14:53:49.570: E/AndroidRuntime(9297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.access$700(ActivityThread.java:141) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.os.Looper.loop(Looper.java:137) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at dalvik.system.NativeStart.main(Native Method) 
01-10 14:53:49.570: E/AndroidRuntime(9297): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.createView(LayoutInflater.java:613) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at com.xxx.xxx.InputFragment.onCreateView(InputFragment.java:70) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.Activity.performStart(Activity.java:5114) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  ... 12 more 
01-10 14:53:49.570: E/AndroidRuntime(9297): Caused by: java.lang.reflect.InvocationTargetException 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at java.lang.reflect.Constructor.constructNative(Native Method) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  ... 27 more 
01-10 14:53:49.570: E/AndroidRuntime(9297): Caused by: java.lang.OutOfMemoryError 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.content.res.Resources.loadDrawable(Resources.java:1965) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.View.<init>(View.java:3330) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.View.<init>(View.java:3259) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.view.ViewGroup.<init>(ViewGroup.java:425) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:210) 
01-10 14:53:49.570: E/AndroidRuntime(9297):  ... 30 more 

我的背景圖像中繪製對象文件夾。是的,我的意思是「文件夾」,因爲該應用程序旨在支持多個屏幕和圖像在所有文件夾中,並且尺寸略有變化。

如何加載這個大的位圖沒有問題?我通過了下面的代碼。

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, 
      matrix, false); 
    return resizedBitmap; 
} 

但是這種情況是,這段代碼手動設置了寬度和高度吧?但是我將把這張圖片作爲背景圖片,所以我不知道這是否是最好的方法,我不確定插入的寬度和高度有什麼價值。我所知道的是我需要在onCreateView()方法(我正在使用片段)中設置圖像。

+0

您是否嘗試過分配7千兆字節的內存?它是否符合您啓動JVM的內存設置? –

+0

我不確定你使用了'resizedBitmap'的正確構造函數。你不需要'Matrix'來進行簡單的大小調整。考慮這個構造函數:http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap,int,int,int,int) – jedwards

+0

你的圖像有什麼分辨率? – Hellboy

回答

0

您需要爲您的JVM分配更多堆(-Xmx7g)或平鋪圖像,以便您不需要一次將整個內容保存在內存中。
當然,如果您沒有一次顯示整個圖像,平鋪只是有用的。如果你是要管理它,你可以在嘗試加載之前以某種方式壓縮圖像。
還有,第三方庫(如DiscMemImage)可以在限制內存使用的同時使用圖像。

1

使用BitmapFactory.Options在加載之前獲取圖像的大小,然後可以將背景縮小爲較低分辨率的圖像(如果需要),該圖像仍然適合背景,但不會引發OOM錯誤。此外,您可以根據所需的寬度和高度,或者您設置背景的視圖的寬度和高度來使用DisplayMetrics。

從Android訓練documation(http://developer.android.com/training/displaying-bitmaps/load-bitmap.html):

的BitmapFactory類提供了多種解碼方法 (decodeByteArray(),decodeFile(),decodeResource(),等等),用於創建 從各個位圖源。根據您的圖像數據源選擇最合適的解碼方法 。這些方法嘗試爲構造的位圖分配內存,因此可能很容易導致OutOfMemory異常。每種類型的解碼方法都有 附加簽名,可讓您通過 BitmapFactory.Options類指定解碼選項。在解碼時將inJustDecodeBounds屬性 設置爲true可避免內存分配,對於位圖對象返回null,但設置爲Width,outHeight和outMimeType。 該技術允許您在構建位圖之前讀取圖像的尺寸和類型(以及內存分配)。

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
String imageType = options.outMimeType; 

爲了避免java.lang.OutOfMemory例外,解碼前檢查 位圖的尺寸,除非你絕對信任源 爲您提供與舒適適合 預見的大小的圖像數據在可用內存中。