如何在 SQLite 数据库中存储视频内容(不是视频路径)

Get*_*tgo 0 android android-sqlite android-database

我想在 sqlite 数据库中存储视频。PS我不想存储路径而是实际的视频内容。我已将视频转换为字节数组并将字节数组存储在 sqlite 数据库中。检索后,bytearray 被转换为文件。但是视频没有播放。请帮忙。

Mik*_*keT 5

我想在 sqlite 数据库中存储视频。PS我不想存储路径而是实际的视频内容。

除非视频很短并且占用的空间很小(比如每个视频最多 200k,可能是 1/10 秒,但取决于保存的格式),否则您可能会遇到问题和异常/崩溃。

  • 使用手机约 2 秒黑色占用 2.2Mb,实际录制视频 2 秒占用 7Mb。

尽管 SQLite 能够按照以下方式存储相对较大的 BLOB:-

  • 字符串或 BLOB 的最大长度

    SQLite 中字符串或 BLOB 的最大字节数由预处理器宏 SQLITE_MAX_LENGTH 定义。该宏的默认值为 10 亿(1 亿或 1,000,000,000)。您可以在编译时使用如下命令行选项提高或降低此值:

    -DSQLITE_MAX_LENGTH=123456789 当前的实现只支持长度最大为 231-1 或 2147483647 的字符串或 BLOB。并且一些内置函数(例如 hex() )可能会在此之前失败。在对安全敏感的应用程序中,最好不要尝试增加最大字符串和 blob 长度。事实上,如果可能的话,您最好将最大字符串和 blob 长度降低到几百万的范围内。

    在 SQLite 的部分 INSERT 和 SELECT 处理期间,数据库中每一行的完整内容都被编码为单个 BLOB。所以 SQLITE_MAX_LENGTH 参数也决定了一行中的最大字节数。

    可以在运行时使用 sqlite3_limit(db,SQLITE_LIMIT_LENGTH,size) 接口降低最大字符串或 BLOB 长度。 SQLite 中的限制

Android SDK 的CursorWindow有 2Mb 的限制,这适用于行的所有列(如果是缓冲区)。因此,即使您可以成功存储视频,您也可能无法检索这些视频。

推荐的方式是你不想要的,即存储视频的路径。

如果我将视频存储在内部/外部存储中并存储路径,那么我将如何从其他设备访问相同的内容。

您会遇到与数据库相同的问题,因为它通常存储在受保护的应用程序数据中。除非数据库是预先存在的数据库(即填充数据),在这种情况下,数据库通过 APK 与应用程序一起分发。

如果后者是通过 APK 分发的预先存在的数据库,那么视频也可以作为 APK 的一部分分发,因此与数据库一样受到保护和公开。

如果您打算在不属于 APK 的设备之间分发视频,那么 SQlite 可能不是正确的解决方案,因为它是一个嵌入式数据库并且没有内置客户端/服务器功能。

此外,如果我的设备被格式化,那么我将丢失所有数据。

在这种情况下,数据库将像任何其他数据一样容易受到攻击,因为数据库就是一个文件,就像视频、word 文档等一样,都需要一个合适的应用程序来查看/更改内容。但是,如果数据库是预先存在的数据库,则只需重新安装应用程序即可从 APK 恢复数据库和其他文件。

工作示例

假设视频将与 APK 一起分发,则使用建议/推荐方法。

创建新项目后,4 个视频被下载并复制到 res/raw 文件夹中(在创建 raw 文件夹后),如下所示:-

在此处输入图片说明

数据库助手(SQLiteOpenHelper 的子类)是为一个带有 - _id列的 2 列表创建的 (注意名为_id用于 SimpleCursorAdapter)。- video_path 用于存储视频的路径/名称(不是完整路径,但足以从存储的数据中确定路径) - 注意 UNIQUE 已被编码以停止添加重复项。

使用一些基本方法来允许添加和删除行以及提取所有行(通过与 SimpleCursorAdapter 一起使用的 Cursor)。

数据库助手

public class DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "myvideos";
    public static final int DBVERSION = 1;

    public static final String TBL_VIDEO = "video";

    public static final String COL_VIDEO_ID = BaseColumns._ID;
    public static final String COL_VIDEO_PATH = "video_path";


    SQLiteDatabase mDB;

    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String crt_video_table = "CREATE TABLE IF NOT EXISTS " + TBL_VIDEO + "(" +
                COL_VIDEO_ID + " INTEGER PRIMARY KEY," +
                COL_VIDEO_PATH + " TEXT UNIQUE" +
                ")";
        db.execSQL(crt_video_table);
    }

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

    }

    public long addVideo(String path) {
        ContentValues cv = new ContentValues();
        cv.put(COL_VIDEO_PATH,path);
        return mDB.insert(TBL_VIDEO,null,cv);
    }

    public Cursor getVideos() {
        return mDB.query(TBL_VIDEO,null,null,null,null,null,null);
    }

    public int deleteVideoFromDB(long id) {
        String whereclause = COL_VIDEO_ID + "=?";
        String[] whereargs = new String[]{String.valueOf(id)};
        return mDB.delete(TBL_VIDEO,whereclause,whereargs);
    }
}
Run Code Online (Sandbox Code Playgroud)

一个非常直接的MainActivity.java(见评论)

public class MainActivity extends AppCompatActivity {

    TextView mMyTextView;
    ListView mVideoList;
    VideoView mVideoViewer;
    DBHelper mDBHlpr;
    Cursor mCsr;
    SimpleCursorAdapter mSCA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMyTextView =  this.findViewById(R.id.mytext);
        mVideoList = this.findViewById(R.id.videolist);
        mVideoViewer = this.findViewById(R.id.videoviewer);

        mDBHlpr = new DBHelper(this);
        addVideosFromRawResourceToDB();
    }

    @Override
    protected void onDestroy() {
        mCsr.close(); //<<<<<<<<<< clear up the Cursor
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        manageListView(); //<<<<<<<<<< rebuild and redisplay the List of Videos (in case they have changed) 
    }

    /**
     *  Setup or Refresh the ListView adding the OnItemClick and OnItemLongClick listeners
     */
    private void manageListView() {
        mCsr = mDBHlpr.getVideos();

        // Not setup so set it up
        if (mSCA == null) {
            // Instantiate the SimpleCursorAdapter
            mSCA = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_1, // Use stock layout
                    mCsr, // The Cursor with the list of videos
                    new String[]{DBHelper.COL_VIDEO_PATH}, // the column (columns)
                    new int[]{android.R.id.text1}, // the view id(s) into which the column(s) data will be placed
                    0 
            );
            mVideoList.setAdapter(mSCA); // Set the adpater for the ListView
            /**
             * Add The Long Click Listener (will delete the video row from the DB (NOT the video))
             */
            mVideoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    mDBHlpr.deleteVideoFromDB(id);
                    manageListView(); // <<<<<<<<<< refresh the ListView as data has changed
                    return true;
                }
            });
            /**
             * Play the respective video when the item is clicked
             * Note Cursor should be at the correct position so data can be extracted directly from the Cursor
             */
            mVideoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    setCurrentVideo(mCsr.getString(mCsr.getColumnIndex(DBHelper.COL_VIDEO_PATH)));
                }
            });
        } else {
            mSCA.swapCursor(mCsr); //<<<<<<<<<< apply the changed Cursor
        }
    }

    /**
     * Set the currrent video and play it
     * @param path the path (resource name of the video)
     */
    private void setCurrentVideo(String path) {

        mVideoViewer.setVideoURI(
                Uri.parse(
                       "android.resource://" + getPackageName() + "/" + String.valueOf(
                               getResources().getIdentifier(
                                       path,
                               "raw",
                               getPackageName())
                       )
                )
        );
        mVideoViewer.start();
    }

    /**
     *  Look at all the resources in the res/raw folder and add the to the DB (not if they are duplicates due to UNQIUE)
     */
    private void addVideosFromRawResourceToDB() {
            Field[] fields=R.raw.class.getFields();
            for(int count=0; count < fields.length; count++){
                Log.i("Raw Asset: ", fields[count].getName());
                mDBHlpr.addVideo(fields[count].getName());
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

刚开始时(没有播放):-

在此处输入图片说明

长按 1Mb 视频(删除 DB 条目)后:-

在此处输入图片说明

单击列表中的视频后:-

在此处输入图片说明