Usman Khan
Usman Khan

Reputation: 3953

How to record a video less than 10 mb using Android

I am working on an Android application in which I am recording a video through native video, the code is given below. Now I want to limit the video size not more than 10M, like during video when it exceeds the limit recording should be stop.

private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
        Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    }


    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
     * returning image / video
     */
    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

Upvotes: 1

Views: 2328

Answers (2)

Mehul Joisar
Mehul Joisar

Reputation: 15358

You need to add one more parameter MediaStore.EXTRA_SIZE_LIMIT while initiating recording,

i.e.:

private void recordVideo() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
    Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

//add one more parameter
    long maxVideoSize = 10*1024*1024; // 10 MB
    intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

Upvotes: 4

Ashish Tamrakar
Ashish Tamrakar

Reputation: 820

You can continuously check the size of file while recording and stop it when size exceeded like this -

public long TotalMemory()//Environment.getExternalStorageDirectory().getAbsolutePath()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long Total = ( (long) statFs.getBlockCount() *  (long) statFs.getBlockSize()) / 1048576;
        return Total;
    }

public long FreeMemory()
{
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    long Free  = (statFs.getAvailableBlocks() *  (long) statFs.getBlockSize()) / 1048576;
    return Free;
}

public long BusyMemory()
{
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
    long Total = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize()) / 1048576;
    long Free  = (statFs.getAvailableBlocks() *  (long) statFs.getBlockSize()) / 1048576;
    long Busy  = Total - Free;
    return Busy;
}

Hope this helps you :)

Upvotes: 0

Related Questions