title: Too big Image: OutOfMemory Exception
body: <p>My background image is too big, so the application crashes with the following error (I am copying other helpful messages from the LogCat as well)</p>
  
  <pre><code>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.&lt;init&gt;(View.java:3330)
  01-10 14:53:48.860: I/dalvikvm(9297):   at android.view.View.&lt;init&gt;(View.java:3259)
  01-10 14:53:48.860: I/dalvikvm(9297):   at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:425)
  01-10 14:53:48.860: I/dalvikvm(9297):   at android.widget.RelativeLayout.&lt;init&gt;(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-&gt;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 &lt;unknown&gt;
  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 &lt;unknown&gt;
  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.&lt;init&gt;(View.java:3330)
  01-10 14:53:49.570: E/AndroidRuntime(9297):     at android.view.View.&lt;init&gt;(View.java:3259)
  01-10 14:53:49.570: E/AndroidRuntime(9297):     at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:425)
  01-10 14:53:49.570: E/AndroidRuntime(9297):     at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:210)
  01-10 14:53:49.570: E/AndroidRuntime(9297):     ... 30 more
  </code></pre>
  
  <p>My background image is in Drawable folders. Yes, I meant "folders" because the application is designed to support multiple screens and images are in all folders, with slight change of sizes.</p>
  
  <p>How can I load this large bitmaps without an issue? I went through the following code.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>But the case is, this code set the width and height manually, right? But I am going to apply this image as the background image, so I don't know whether this is the best way, and I am not sure what value to insert as width and height. All I know is I need to set the image in <code>onCreateView()</code> method (I am using fragments).</p>