title: Loading large images without OutOfMemoryError
body: <p>I have a 5000 x 4000 px image which I want to draw onto a canvas.</p>
  
  <p>First I tried to load it from resources.
  I put it in <code>/res/drawable</code>.</p>
  
  <p>I used the following method:</p>
  
  <pre><code>InputStream input = getResources().openRawResource(R.drawable.huge_image);
  Drawable d = Drawable.createFromStream(input, "image");
  d.setBounds(...);
  d.draw(canvas);
  </code></pre>
  
  <p>It worked like a charm.</p>
  
  <p>In this case the <code>InputStream</code> is an <code>AssetManager.AssetInputStream</code>.</p>
  
  <p>So now I want to load it from the sdcard.</p>
  
  <p>Here's what I tried to do:</p>
  
  <pre><code>File f = new File(path);
  Uri uri = Uri.fromFile(f);
  InputStream input = mContext.getContentResolver().openInputStream(uri);
  Drawable d = Drawable.createFromStream(input, "image");
  </code></pre>
  
  <p>In this case the <code>InputStream</code> is a <code>FileInputStream</code> and I got an <code>OutOfMemoryError</code> when creating the <code>Drawable</code>.</p>
  
  <p>So I'm wondering:</p>
  
  <p>Is there a way to load the image without getting that error? Or is there a way to convert a <code>FileInputStream</code> to an <code>AssetInputStream</code> ?</p>
  
  <p>Note:</p>
  
  <p>I don't want to resize the image because I'm implementing zoom/pan functionality.
  Please don't tell me to read <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html">Loading Large Bitmaps Efficiently</a>.</p>
  
  <p>You can check out the full class <a href="https://github.com/BenitoBertoli/ZoomImageView/blob/master/src/com/benitobertoli/largeimagezoom/ZoomImageView.java">here</a>. The error occurs when using <code>setImageUri()</code>.</p>
  
  <p>Here's my Error Log:</p>
  
  <pre><code>08-13 11:57:54.180: E/AndroidRuntime(23763): FATAL EXCEPTION: main
  08-13 11:57:54.180: E/AndroidRuntime(23763): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:468)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:332)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.benitobertoli.largeimagezoom.ZoomImageView.setDrawablefromUri(ZoomImageView.java:187)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.benitobertoli.largeimagezoom.ZoomImageView.setImageUri(ZoomImageView.java:588)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.benitobertoli.largeimagezoom.TestActivity.onKeyDown(TestActivity.java:30)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.view.KeyEvent.dispatch(KeyEvent.java:1257)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.app.Activity.dispatchKeyEvent(Activity.java:2075)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1673)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2493)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2463)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.os.Handler.dispatchMessage(Handler.java:99)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.os.Looper.loop(Looper.java:144)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at android.app.ActivityThread.main(ActivityThread.java:4937)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at java.lang.reflect.Method.invokeNative(Native Method)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at java.lang.reflect.Method.invoke(Method.java:521)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  08-13 11:57:54.180: E/AndroidRuntime(23763):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I was testing my code on an HTC Desire A8181.
  After being told that the first code snippet wasn't working on some other devices, I tested on a Samsung Galaxy S2 and on the Emulator.</p>
  
  <p>Results:
  When loading from resources, the emulator gave an <code>OutOfMemoryError</code>, the Galaxy S2 didn't throw an exception but the returned <code>Drawable</code> was null.</p>
  
  <p>So I guess for the time being the only solution is to downsample the image.</p>