Android 循环录像,保留加锁视频

○-○–○—○----○-----○------○ 呆萌的分割线 ○------○-----○----○—○–○-○

言归正传,总结一下Android循环录像的逻辑:

1.保存视频时,存储视频信息到数据库,包括:

id视频ID
name视频名称
lock是否锁定(0,1)
resolution分辨率(720,1080)

2.每次开始录像和保存视频时检查剩余存储空间

sdFree < sdTotal * Constant.Record.SD_MIN_FREE_PERCENT || intSdFree < Constant.Record.SD_MIN_FREE_STORAGE

3.如果低于预设底限,则开始删除较旧的未加锁视频

4.如果删除所有的未加锁视频后,空间依然不足,则开始删除加锁视频

5.如果删除所有的加锁视频,空间依然不足,提示用户清理SD卡

[ Well well. Talk is cheap. Show me the code. ]

完整代码:

/**

  • 获得SD卡总大小

  • @return 总大小,单位:字节B

*/

public static long getSDTotalSize(String SDCardPath) {

StatFs stat = new StatFs(SDCardPath);

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

return blockSize * totalBlocks;

}

/**

  • 获得sd卡剩余容量,即可用大小

  • @return 剩余空间,单位:字节B

*/

public static long getSDAvailableSize(String SDCardPath) {

// StatFs stat = new StatFs(“/storage/sdcard1”);

StatFs stat = new StatFs(SDCardPath);

long blockSize = stat.getBlockSize();

long availableBlocks = stat.getAvailableBlocks();

return blockSize * availableBlocks;

}

/**

  • 录像SD卡是否存在

  • @return

*/

public static boolean isVideoCardExists() {

try {

String pathVideo = Constant.Path.SDCARD_1 + “/tachograph/”;

if (Constant.Record.saveVideoToSD2) {

pathVideo = Constant.Path.SDCARD_2 + “/tachograph/”;

}

File fileVideo = new File(pathVideo);

fileVideo.mkdirs();

File file = new File(pathVideo);

if (!file.exists()) {

return false;

}

} catch (Exception e) {

MyLog.e(“[StorageUtil]isVideoCardExists:Catch Exception!”);

return false;

}

return true;

}

package com.tchip.carlauncher.model;

import java.util.ArrayList;

import java.util.List;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DriveVideoDbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = “video_db”;

private static final String VIDEO_TABLE_NAME = “video”;

private static final String VIDEO_COL_ID = “_id”;

private static final String VIDEO_COL_NAME = “name”; // 视频文件名称,如:2015-07-01_105536.mp4

private static final String VIDEO_COL_LOCK = “lock”; // 是否加锁:0-否 1-是

private static final String VIDEO_COL_RESOLUTION = “resolution”; // 视频分辨率:720/1080

private static final String[] VIDEO_COL_PROJECTION = new String[] {

VIDEO_COL_ID, VIDEO_COL_NAME, VIDEO_COL_LOCK, VIDEO_COL_RESOLUTION, };

public DriveVideoDbHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

// Create table

@Override

public void onCreate(SQLiteDatabase db) {

String createRouteTableSql = “CREATE TABLE " + VIDEO_TABLE_NAME + " (”

  • VIDEO_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"

  • VIDEO_COL_NAME + " TEXT," + VIDEO_COL_LOCK + " INTEGER,"

  • VIDEO_COL_RESOLUTION + " INTEGER" + “);”;

db.execSQL(createRouteTableSql);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// Drop older table if existed

db.execSQL("DROP TABLE IF EXISTS " + VIDEO_TABLE_NAME);

// Create tables again

onCreate(db);

}

// Add new DriveVideo

public int addDriveVideo(DriveVideo driveVideo) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(VIDEO_COL_NAME, driveVideo.getName());

values.put(VIDEO_COL_LOCK, driveVideo.getLock());

values.put(VIDEO_COL_RESOLUTION, driveVideo.getResolution());

// Insert to database

long rowId = db.insert(VIDEO_TABLE_NAME, null, values);

// Close the database

db.close();

return (int) rowId;

}

// Get DriveVideo By ID

public DriveVideo getRouteDistanceById(int id) {

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(VIDEO_TABLE_NAME, VIDEO_COL_PROJECTION,

VIDEO_COL_ID + “=?”, new String[] { String.valueOf(id) }, null,

null, null, null);

if (cursor != null)

cursor.moveToFirst();

DriveVideo driveVideo = new DriveVideo(cursor.getInt(0),

cursor.getString(1), cursor.getInt(2), cursor.getInt(3));

db.close();

cursor.close();

return driveVideo;

}

// Get DriveVideo By Name

public DriveVideo getDriveVideoByName(String name) {

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(VIDEO_TABLE_NAME, VIDEO_COL_PROJECTION,

VIDEO_COL_NAME + “=?”, new String[] { name }, null, null, null,

null);

if (cursor != null)

cursor.moveToFirst();

DriveVideo driveVideo = new DriveVideo(cursor.getInt(0),

cursor.getString(1), cursor.getInt(2), cursor.getInt(3));

db.close();

cursor.close();

return driveVideo;

}

public boolean isVideoExist(String name) {

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(VIDEO_TABLE_NAME, VIDEO_COL_PROJECTION,

VIDEO_COL_NAME + “=?”, new String[] { name }, null, null, null,

null);

if (cursor.getCount() > 0) {

db.close();

cursor.close();

return true;

} else {

db.close();

cursor.close();

return false;

}

}

/**

  • 根据视频名查询是否加锁

  • @param name

  • @return

*/

public int getLockStateByVideoName(String name) {

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(VIDEO_TABLE_NAME, VIDEO_COL_PROJECTION,

VIDEO_COL_NAME + “=?”, new String[] { name }, null, null, null,

null);

if (cursor.getCount() > 0) {

cursor.moveToFirst();

int videoLock = cursor

.getInt(cursor.getColumnIndex(VIDEO_COL_LOCK));

db.close();

cursor.close();

return videoLock;

} else {

db.close();

cursor.close();

return 0;

}

}

/**

  • 获取所有的视频信息

  • @return

*/

public List getAllDriveVideo() {

List driveVideoList = new ArrayList();

String selectQuery = "SELECT * FROM " + VIDEO_TABLE_NAME;

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list

if (cursor.moveToFirst()) {

do {

DriveVideo driveVideo = new DriveVideo(cursor.getInt(0),

cursor.getString(1), cursor.getInt(2), cursor.getInt(3));

driveVideoList.add(driveVideo);

} while (cursor.moveToNext());

}

db.close();

cursor.close();

// return list

return driveVideoList;

}

public Cursor getAllDriveVideoCursor() {

String selectQuery = "SELECT * FROM " + VIDEO_TABLE_NAME;

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(selectQuery, null);

return cursor;

}

/**

  • 获取加锁视频Cursor

  • @return

*/

public Cursor getLockVideoCursor() {

String sqlLine = "SELECT * FROM " + VIDEO_TABLE_NAME + " WHERE "

  • VIDEO_COL_LOCK + “=?”;

String selection[] = new String[] { “1” };

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(sqlLine, selection);

return cursor;

}

/**

  • 获取最旧且未加锁视频ID

  • @return

*/

public int getOldestUnlockVideoId() {

String sqlLine = "SELECT * FROM " + VIDEO_TABLE_NAME + " WHERE "

  • VIDEO_COL_LOCK + “=?”;

String selection[] = new String[] { “0” };

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(sqlLine, selection);

if (cursor.getCount() > 0) {

cursor.moveToFirst();

int id = cursor.getInt(cursor.getColumnIndex(VIDEO_COL_ID));

db.close();

cursor.close();

return id;

} else {

db.close();

cursor.close();

return -1;

}

}

/**

  • 获取最旧视频(包括加锁)ID

  • @return

*/

public int getOldestVideoId() {

String sqlLine = "SELECT * FROM " + VIDEO_TABLE_NAME;

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(sqlLine, null);

if (cursor.getCount() > 0) {

cursor.moveToFirst();

int id = cursor.getInt(cursor.getColumnIndex(VIDEO_COL_ID));

db.close();

cursor.close();

return id;

} else {

db.close();

cursor.close();

return -1;

}

}

public String getVideNameById(int id) {

String sqlLine = "SELECT * FROM " + VIDEO_TABLE_NAME + " WHERE "

  • VIDEO_COL_ID + “=?”;

String selection[] = new String[] { String.valueOf(id) };

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery(sqlLine, selection);

if (cursor.getCount() > 0) {

cursor.moveToFirst();

String videoName = cursor.getString(cursor

.getColumnIndex(VIDEO_COL_NAME));

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

getColumnIndex(VIDEO_COL_NAME));

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-oQuuuZ0R-1714950908280)]

[外链图片转存中…(img-7ID96KNZ-1714950908280)]

[外链图片转存中…(img-0RkWbYFn-1714950908280)]

[外链图片转存中…(img-BUEIhf3R-1714950908281)]

[外链图片转存中…(img-QI3CAf3n-1714950908281)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

  • 28
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值