How to check for device memory : For the ANDROID Developer

8:20 PM

Okay so you are implementing your own Camera app and everything works perfectly well. But has your code checked as to whether there is enough space on your device for it to actually take videos?

If you haven't, then here is a small easy code snippet that you can add into your code to make sure your camera app doesn’t fail you even when there’s no memory left on your device.

Basically your app will keep recording a video for you but the moment you stop recording, BOOM! You don’t have a file with you. Neither your device nor your app alerted the user about there being no memory on your device(SD Card) , and it was all in vain that he/she tried to record something.

This is a quick way to help you check for the device space even before you start videoing.

public boolean isMemoryLow() {

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  MemoryInfo memInfo = new ActivityManager.MemoryInfo();
  
//Retrieving available memory 
  actManager.getMemoryInfo(memInfo);
  
//Avail memory is the memory left on your system. 
//Dividing by 1048576L will give you the 
  //amount of megabytes.
  long availableMegs = memInfo.availMem / 1048576L;
  
  //memory is alerted if device has less than 10 megabytes
  if (availableMegs < 10)
   return true;
  else
   return false;  
 }
Insert the above code into the place where you start a video recording. It will prompt you as to whether your device has space, in this case 10Mb to allow you to start recording.

Note : Android does have an onLowMemory callback but as per the documentation... This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. While the exact point at which this will be called is not defined, generally it will happen around the time all background process have been killed, that is before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing. 

You Might Also Like

0 comments