How to calculate remaining photos counts in custom camera in android

Viewed 537

I'm trying to calculate the remaining number of photos that can be taken using my custom camera and show that count to the user. I tried with the following code:

private void numberOfPhotosAvailable() {
        long photosAvailable = 0;
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        resolution=getResolution();
        long bytesPerPhoto=resolution/1048576;
        long bytesAvailable = (long) stat.getAvailableBlocksLong() * (long) stat.getBlockSizeLong();
        long megAvailable = bytesAvailable / 1048576;

        System.out.println("Megs :" + megAvailable);
        photosAvailable = megAvailable / bytesPerPhoto;
        tvAvailablePhotos.setText("" + photosAvailable);
    }

Method for getting resolution.

public long getResolution(){
        long resolution=0;
        Camera.Parameters params=mCamera.getParameters();
        List<Camera.Size> sizes = params.getSupportedPictureSizes();

        Camera.Size size = sizes.get(0);
        int width=size.width;
        int height=size.height;
        resolution=width*height;
       return resolution;
    }

PROBLEM: There is a lot of difference in the count shown in the phone's camera and count that is shown in my app.

So what is the proper way of doing this ?

NOTE: I will only be capturing image in the highest quality available. Therefore I am only calculating count according to one resolution only.

4 Answers

I think you can check the DCIM directory(Default Camera Directory) for the number of files and calculate the size of all the files and get the average size by dividing the number of files.

and you will get the average size camera is capturing the images do above steps in Asynctask.

and you already calculated the remaining size in bytes now divide again the remaining size with the average size and you will get the approx number of images you can capture.

Related