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

时间:2019-02-15 10:27:58

标签: android android-sqlite android-database

我想将视频存储在sqlite数据库中。附言我不想存储路径,但要存储实际的视频内容。 我已经将视频转换为字节数组并将字节数组存储在sqlite数据库中。在检索时,字节数组将转换为File。但是视频没有播放。请帮忙。

2 个答案:

答案 0 :(得分:2)

  

我想将视频存储在sqlite数据库中。附言我不想   存储路径,但存储实际的视频内容。

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

  • 使用一部手机大约2秒钟黑色会占用2.2Mb,而实际录制视频2秒钟会占用7Mb。

尽管SQLite能够按照:-

存储相对较大的BLOB。
  •   

    字符串或BLOB的最大长度

         

    在SQLite中定义字符串或BLOB中的最大字节数   通过预处理程序宏SQLITE_MAX_LENGTH。这个的默认值   宏为10亿(十亿或十亿)。您可以   使用命令行选项在编译时提高或降低此值   像这样:

         

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

         

    在SQLite的INSERT和SELECT处理的一部分中,完整的   数据库中每一行的内容被编码为单个BLOB。所以   SQLITE_MAX_LENGTH参数还确定最大数量   连续字节。

         

    可以使用以下命令在运行时降低最大字符串或BLOB长度   sqlite3_limit(db,SQLITE_LIMIT_LENGTH,size)接口。   Limits In SQLite

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

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

  

如果我将视频存储在内部/外部存储中并存储   路径,然后我将如何能够从一些访问相同   其他设备。

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

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

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

  

如果我的设备格式化了,那我将丢失所有   数据。

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

工作示例

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

在创建新项目后,按照以下步骤下载了4个视频并将其复制到res / raw文件夹(在创建原始文件夹之后):-

enter image description here

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

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

DBHelper.java

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);
    }
}

漂亮的 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());
            }
    }
}

结果

第一次开始(什么都没玩):-

enter image description here

长时间单击1Mb视频(删除数据库条目)后:-

enter image description here

单击列表中的视频后:-

enter image description here

答案 1 :(得分:0)

您可以使用这种方法

保存视频时,将其保存在应用程序的私有存储文件夹中。

Context.getFilesDir()

这将为您提供..\Andorid\data\data\com.example.app中应用存储的路径 它将存储在内部存储中。

其中com.example.app是您的应用程序包ID。您可以在此处创建一个新文件夹,例如Videos,然后将视频保存在此文件夹中。将其路径保存在数据库中。只有您的应用可以访问此文件夹。没有任何其他应用程序或设备用户可以访问此文件夹。因此,除了您的应用程序之外,没有人可以编辑或删除您的文件。

此外,如果用户重置了手机,则在某些情况下,这些数据以及您的数据库以及您的应用也会被删除。因此,无需担心会删除您的文件,但数据库仍然具有它们的路径。如果文件被删除,则数据库也将被删除,但仅在应用Uninstall, device resetSD card erase时删除。