Jump to content
  • Advertisement
Sign in to follow this  

OutOfMemory error while loading bitmaps / camera capture Bitmaps

This topic is 1784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I need your expertise help.


I have been stuck with OutOfMemory error for a long while. For some time I concentrated on other parts of the program, but now i have to get it sorted. Seaching through the web extensively, particularly on stackoverflow i realised its one of the most common problems talked about and i thought i won't be long before i get it sorted from the numerous solutions suggested, but how wrong i was.


dalvikvm-heap                Out of memory on a 2834945-byte allocation.

Camera-JNI                   Couldn't allocate byte array for JPEG data


I tralled through suggested solutions for weeks but to no avail.


Crucially i couldn't adapt the solution on


http://developer.android.com/training/displaying-bitmaps/load-bitmap.html and others suggested solutions to my own code.


These all expect you are loading bitmap from the resource hence reflected in the parameters




public static Bitmap 
decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)

I was using bitmap from live camera and also loading bitmap from the sdcard.

The whole program is too large to place here but here are the relevant parts, sufficient to see to the problem.

The camera captures live photos in cycles. After processing each bitmap I expect to discard it and process the next capture. I kept having OOM errors. Then I used a loaded bitmap from sdcard in its place so that that problem wouldn't slow me down. Even then i stll have OOM errors . I have to admit the size of the loaded bitmap === to captured live bitmap is huge 3264 X 2448 But the point is all the inbuilt code solution to manage this size and scale it down effeciently have failed to work. I hope your expertise would be able to see a solution, thanks

extract relevant code only so it can be easy to follow

byte[] tempdata;
String     CAMERA_PIC_DIR,ImageDir;
public Bitmap  directCameraSaveBM1;

ImageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + CAMERA_PIC_DIR;


in code below

directCameraSaveBM1   =   BitmapFactory.decodeFile(ImageDir+"useThisPhoto10aa.jpg");                 //loaded bitmap..


bm.recycle();                                   //replacing this live bitmap, but this is the real one i intend to use eventually except that it fails, loaded also fails OOM

bm = null;

	PictureCallback mjpeg = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera c) {
			if(data !=null) {

	void done( ) {
		BitmapFactory.Options options = new BitmapFactory.Options();   //for direct cam capture
		options.inSampleSize = 8;        
		bm = BitmapFactory.decodeByteArray(tempdata,  0, tempdata.length, options); 
		int pNum=11;
		directCameraSaveBM1 = BitmapFactory.decodeFile(ImageDir+"useThisPhoto10aa.jpg");///remove comment
//		float	px = (float) getBaseContext().getResources().getDisplayMetrics().densityDpi/160f;//
		bm.recycle();   bm  = null;

		parameters = mCamera.getParameters();                    
		previewsize = parameters.getPreviewSize();               
		rWidth = previewsize.width;                  //800  480
   	    rHeight = previewsize.height;                //480  180   
   	    miniW = noticeButton4.getWidth();                             
   	    miniH = noticeButton4.getHeight();
   	    Log.v("rWidth  "+Integer.toString(rWidth), "   rHeight   "+Integer.toString(rHeight));
   	    Globals.bm3G = Bitmap.createScaledBitmap(directCameraSaveBM1, rWidth, rHeight, false);   

    	lH = ((rHeight/2) - (miniH/2));
    	uH = ((rHeight/2) + (miniH/2));
    	lW = ((rWidth/2) - (miniW/2));
    	uW = ((rWidth/2) + (miniW/2));

        //// a lot of crap code follows......










                     /// lots of image processing code and stuff
	    save_bm_image( ImageDir, pNum );/**/
    } //done()

	void save_bm_image( String bname, int choose ){
		if     ( choose == 3 )     fname = fname3;
		else if( choose == 4 )     fname = fname4;
		else if( choose == 5 )     fname = fname5;
		File file = new File (bname, fname);   ///saving bitmap to file
		if (file.exists ()) file.delete (); 
		try {
		       FileOutputStream out = new FileOutputStream(file);
		       Globals.bm3G.compress(Bitmap.CompressFormat.PNG, 90, out);
		} catch (Exception e) {
		name = bname + fname;

I hope someone can spot why I have this problem and offer code solution tips

 Lots of thanks for any help

Edited by run_g

Share this post

Link to post
Share on other sites

You might find this thread interesting: http://www.gamedev.net/topic/630698-android-out-of-memory-when-loading-images/



my apologies...  may be my thread topic "OOM" overlaps with that thread and that's it... cannot find solution there. Like I said in the details of my thread I have read many solutions on stackoverflow (and elsewhere) that haven't worked for my application, ....





 many thanks

Edited by run_g

Share this post

Link to post
Share on other sites

Maybe you reached the heap memory limit and have to do it natively in c++


Can you give further clues on how to do this... I'm still in the learning process. Many thanks

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!