我的背景圖片太大,所以,出現以下錯誤的應用程序崩潰(我從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()
方法(我正在使用片段)中設置圖像。
您是否嘗試過分配7千兆字節的內存?它是否符合您啓動JVM的內存設置? –
我不確定你使用了'resizedBitmap'的正確構造函數。你不需要'Matrix'來進行簡單的大小調整。考慮這個構造函數:http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap,int,int,int,int) – jedwards
你的圖像有什麼分辨率? – Hellboy