Skip to content Skip to sidebar Skip to footer

Checking Image Size Before It Can Be Saved To Server

Good day, please I am trying to write an application that stores images I take with Camera automatically to a database. However i want to check the image size(MB) before it can sav

Solution 1:

If you are using a custom camera then in your callback you's receive a byte array just get the length of this array and divide it by 1024*1024 to get size in MB.

If you are using the Camera intent then you get back a Bitmap whose size can be found by using bitmap.getRowbytes * bitmap.getHeight and then divide it by 1024*1024 to get image size in MB


Solution 2:

getRowBytes() * getHeight() seems to be working fine to me.


Solution 3:

Your server cannot know the size of the image unless it can look at it. You can use PHP to cap the maximum upload size. Once you have the file on the server as a temporary file, you can analyse it in more detail (dimensions, etc). If the image is "good", then move it to it's permanent location (another directory or as database BLOB), if not then just delete it.


Solution 4:

In your cameras PictureCallback:

public void onPictureTaken(byte[] data, Camera camera)
{
  int nPictureSize = data.length;
}

the length of the compressed image is simply the length of the byte array


Post a Comment for "Checking Image Size Before It Can Be Saved To Server"