-1

I am trying to read an image from the resource folder in my app and put it in a bitmap so i can do with it what i need. When reading images larger than ~ 1mb i get this:

E/Binder  ( 3568): java.lang.OutOfMemoryError
E/Binder  ( 3568):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/Binder  ( 3568):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
E/Binder  ( 3568):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
E/Binder  ( 3568):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)

E/JavaBinder( 3568): Caused by: java.lang.OutOfMemoryError
E/JavaBinder( 3568):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/JavaBinder( 3568):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
E/JavaBinder( 3568):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
E/JavaBinder( 3568):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)

Is there a safer way to get these kind of images ? And i do not want to scale them down i just need to read the image as is.

Edit 1:

i am using this code :

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), id, options);

options.inSampleSize = 1;
options.inScaled = false;
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeResource(res, id, options);

i do not want to downsize the inSampleSize. i want to preserve the initial resolution but put the image into a bitmap. Can this be done with images above 2 mb in size and a width of 7000 px and height of 2400 px ?

Edit 2:

I've managed to bypass the error by using android:largeHeap="true", but surely there should be another solution to this ...

John11
  • 437
  • 1
  • 6
  • 16
  • Go through this [developers guide](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) – Sagar Pilkhwal Sep 24 '14 at 10:06
  • @SagarPilkhwal I read the guide and used code from there. That guide explains how to load large pictures to be used showed as smaller pics. but I need to read the picture like i had inSampleSize = 1 – John11 Sep 24 '14 at 10:10
  • 400kb images can take upto 10-15Mb of ram, i had similar problem, i simply resized the image to lower resolution, do you want to perform any bitmap specific operations on the image or just file byte[] related operation, because `decodeStream()` requires large amount of ram – Sagar Pilkhwal Sep 24 '14 at 10:11
  • @SagarPilkhwal I understand what you say but i cannot and do not want to control the size of the original pics. it can be as large as 10 mb for pic. – John11 Sep 24 '14 at 10:13
  • you can use `BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3;` to sample the image – Sagar Pilkhwal Sep 24 '14 at 10:15
  • when you talk about 10mb images, its just the file size, but you are decoding the bitmap, so when you decode the bitmap the memory required is `width*height* 4bytes` so calculate the max resolution of image you want to support with this formula and check the ram required – Sagar Pilkhwal Sep 24 '14 at 10:19

1 Answers1

0

First scale the image to lower resolution before using that image

   use BitmapFactory.Options with BitmapFactory.decode* function(params),

using 'inDensity' and 'inTargetDensity' Example: if you have 1600*1200 resolution and want to resize to 640*480 resolution then 'inDensity' = 5 and 'inTargetDensity' = 2 (1600*2 equal to 640*5).

http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

refer the above link

Ruban
  • 1,514
  • 2
  • 14
  • 21
  • i do not want to resize my image just get it into android's memory. Can it be done? assuming android has enough ram to do it. – John11 Sep 24 '14 at 10:46