Sign in to follow this  

OutOfMemory error while loading bitmaps / camera capture Bitmaps

This topic is 1441 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;

CAMERA_PIC_DIR = "/DCIM/Camera/";
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) {
			mCamera.startPreview();
			
			if(data !=null) {
				tempdata=data;
				done();
			}
		}
	};
	

	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 );/**/
	    Runtime.getRuntime().gc();
    } //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);
		       out.flush();
		       out.close();	
		} catch (Exception e) {
		       e.printStackTrace();
		}	
		
		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, ....

 

PLEASE I'M STILL URGENTLY SEEKING A SOLUTION

 

 

 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

This topic is 1441 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this