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

/**

  • 获得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));

db.close();

cursor.close();

return videoName;

} else {

db.close();

cursor.close();

return “”;

}

}

public int updateDriveVideo(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());

return db.update(VIDEO_TABLE_NAME, values, VIDEO_COL_ID + “=?”,

new String[] { String.valueOf(driveVideo.getId()) });

}

public void deleteDriveVideoById(int driveVideoId) {

SQLiteDatabase db = this.getWritableDatabase();

db.delete(VIDEO_TABLE_NAME, VIDEO_COL_ID + “=?”,

new String[] { String.valueOf(driveVideoId) });

db.close();

}

public void deleteDriveVideoByName(String name) {

SQLiteDatabase db = this.getWritableDatabase();

db.delete(VIDEO_TABLE_NAME, VIDEO_COL_NAME + “=?”,

new String[] { name });

db.close();

}

public void deleteAllDriveVideo() {

SQLiteDatabase db = this.getWritableDatabase();

db.delete(VIDEO_TABLE_NAME, null, null);

db.close();

}

}

不过,函数写这么长真的好么?

/**

  • 删除最旧视频

*/

private boolean deleteOldestUnlockVideo() {

try {

String sdcardPath = Constant.Path.SDCARD_1 + File.separator;// “/storage/sdcard1/”;

if (Constant.Record.saveVideoToSD2) {

sdcardPath = Constant.Path.SDCARD_2 + File.separator;// “/storage/sdcard2/”;

}

// sharedPreferences.getString(“sdcardPath”,“/mnt/sdcard2”);

float sdFree = StorageUtil.getSDAvailableSize(sdcardPath);

float sdTotal = StorageUtil.getSDTotalSize(sdcardPath);

int intSdFree = (int) sdFree;

MyLog.v(“[deleteOldestUnlockVideo] sdFree:” + intSdFree);

while (sdFree < sdTotal * Constant.Record.SD_MIN_FREE_PERCENT

|| intSdFree < Constant.Record.SD_MIN_FREE_STORAGE) {

int oldestUnlockVideoId = videoDb.getOldestUnlockVideoId();

// 删除较旧未加锁视频文件

if (oldestUnlockVideoId != -1) {

String oldestUnlockVideoName = videoDb

.getVideNameById(oldestUnlockVideoId);

File f = new File(sdcardPath + “tachograph/”

  • oldestUnlockVideoName.split(“_”)[0]

  • File.separator + oldestUnlockVideoName);

if (f.exists() && f.isFile()) {

MyLog.d(“Delete Old Unlock Video:” + f.getName());

int i = 0;

while (!f.delete() && i < 5) {

i++;

MyLog.d(“Delete Old Unlock Video:” + f.getName()

  • " Filed!!! Try:" + i);

}

}

// 删除数据库记录

videoDb.deleteDriveVideoById(oldestUnlockVideoId);

} else {

int oldestVideoId = videoDb.getOldestVideoId();

if (oldestVideoId == -1) {

/**

  • 有一种情况:数据库中无视频信息。导致的原因:

  • 1:升级时选Download的话,不会清理USB存储空间,应用数据库被删除; 2:应用被清除数据

  • 这种情况下旧视频无法直接删除, 此时如果满存储,需要直接删除

*/

File file = new File(sdcardPath + “tachograph/”);

StorageUtil.RecursionDeleteFile(file);

MyLog.e(“!!! Delete tachograph/ Directory”);

sdFree = StorageUtil.getSDAvailableSize(sdcardPath);

intSdFree = (int) sdFree;

if (sdFree < sdTotal

  • Constant.Record.SD_MIN_FREE_PERCENT

|| intSdFree < Constant.Record.SD_MIN_FREE_STORAGE) {

// 此时若空间依然不足,提示用户清理存储(已不是行车视频的原因)

MyLog.e(“Storage is full…”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值