title: Android out-of-memory crash relating to configuration of drawable resource direcories
body: <p>Originally everything was fine.  I had been working with the 5 drawable directories</p>
  
  <pre><code>drawable-hdpi
  drawable-ldpi
  drawable-mdpi
  drawable-xhdpi
  drawable-xxhdpi
  </code></pre>
  
  <p>that were created for me by the eclipse adt-bundle when I created a new project recently.  So when I needed a new bitmap, I had been putting a copy of the bitmap into each of these 5 directories.  This always seemed a bit strange to me, so I was happy to discover by reading <a href="http://developer.android.com/guide/topics/resources/providing-resources.html" rel="nofollow">the Android documentation <em>Providing Resources</em></a> that I could have a single directory 'drawable' for all the common bitmaps.  So I switched to</p>
  
  <pre><code>drawable
  drawable-hdpi
  drawable-ldpi
  drawable-mdpi
  drawable-xhdpi
  drawable-xxhdpi
  </code></pre>
  
  <p>The only file that was left in the original 5 directories was the 'ic_launcher.png' file had been created for me when the project was originally created.</p>
  
  <p>However, the new configuration of drawable directories failed badly.  Loading up a 36k bitmap file frame.png, an out-of-memory error now occurs as a result of the line</p>
  
  <pre><code> bitmap_fancyframe = BitmapFactory.decodeResource(getResources(), R.drawable.frame);
  </code></pre>
  
  <p>The logcat shows that Android now tries to allocate a 6790156 byte memory block as a result that line (which is the line DrawOnTop.java:95 mentioned at the bottom of the logcat)</p>
  
  <pre><code>06-01 07:26:53.995: I/dalvikvm-heap(1530): Forcing collection of SoftReferences for 6790156-byte allocation
  06-01 07:26:54.025: D/dalvikvm(1530): GC_BEFORE_OOM freed 9K, 14% free 53820K/62343K, paused 27ms, total 27ms
  06-01 07:26:54.025: E/dalvikvm-heap(1530): Out of memory on a 6790156-byte allocation.
  06-01 07:26:54.025: I/dalvikvm(1530): "main" prio=5 tid=1 RUNNABLE
  06-01 07:26:54.025: I/dalvikvm(1530):   | group="main" sCount=0 dsCount=0 obj=0x41077508 self=0x40de49a0
  06-01 07:26:54.025: I/dalvikvm(1530):   | sysTid=1530 nice=0 sched=0/0 cgrp=apps handle=1075179312
  06-01 07:26:54.025: I/dalvikvm(1530):   | schedstat=( 681383064 1229707806 4777 ) utm=53 stm=14 core=0
  06-01 07:26:54.025: I/dalvikvm(1530):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
  06-01 07:26:54.025: I/dalvikvm(1530):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
  06-01 07:26:54.025: I/dalvikvm(1530):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
  06-01 07:26:54.025: I/dalvikvm(1530):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:499)
  06-01 07:26:54.025: I/dalvikvm(1530):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:529)
  06-01 07:26:54.025: I/dalvikvm(1530):   at com.example.MyApp.DrawOnTop.&lt;init&gt;(DrawOnTop.java:95)
  </code></pre>
  
  <p>Can anyone explain why this happens?  More importantly, that frame.png file gets scaled and stretched in my code so I really don't need 5 copies of it, so is there any way that I can have all common png bitmaps in the one 'drawable' directory without these kind of problems?</p>
  
  <p><b>Update</b> Thanks to a comment from @sam and an answer from @ken-wolf, I now understand that Android does a lot of bitmap scaling if a bitmap of the right type isn't available.  So when I simply copy the same bitmaps between all directories, my new understanding is that the effect of this is that the bitmap will be loaded without scaling.  Something is clearly going wrong in that scaling process for me, but since I'm doing my own scaling, I don't need Android to do it too.  So I now think the important question is: <b>How can I setup my app so that just one version of each bitmap is provided, and instruct Android not to do any bitmap scaling</b>.</p>