在我的应用程序中,我希望数据库包含一些视频。
是否可以在数据库中存储视频?
如果可以给我示例链接。
答案 0 :(得分:8)
将视频文件存储在数据库中是不好的做法不方便。将视频文件存储在外部存储或内部存储中,并将这些视频文件的路径存储在数据库中。因此,您可以从数据库快速访问您的数据。它也有助于您的应用程序性能。
答案 1 :(得分:0)
package database;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "your_db_name.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private String DB_PATH;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
this.DB_PATH = Environment.getDataDirectory() + "/data/"
+ myContext.getPackageName() + "/databases/";
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (dbExist) {
Log.i("DataBase", "do nothing - database already exist");
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDataBase();
Log.i("DataBase", "Copy DataBase");
} catch (Exception e) {
// TODO: handle exception
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
// this path value is coming correct?
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
*
* @throws IOException
* */
private void copyDataBase() {
// Open your local db as the input stream
try {
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public void deleteEntry(String table_name ,String whereclause ,String[] field_value) throws SQLException {
// delete the database
myDataBase.delete(table_name, whereclause, field_value);
}
public void insertEntry(String table_name ,ContentValues field_value) throws SQLException {
// insert the database
myDataBase.insert(table_name, null, field_value);
}
public void updateEntry(String table_name ,ContentValues field_value,String whereClause,String[] whereArgs) throws SQLException {
// insert the database
myDataBase.update(table_name, field_value, whereClause, whereArgs);
}
@Override
public void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public Cursor fetchDataFromDataBase(String query) {
Cursor cursor = null;
if (myDataBase != null) {
cursor = myDataBase.rawQuery(query, null);
}
return cursor;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
列出项目
// and call from your application level class when your application launche first
// then use your database by using sq-lite query
myDbHelper = new DataBaseHelper(this);
try {
// calling from here to creat databases from assets
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
列出项目
// just use query like that
// calling from here to open databases from assets
DataBaseHelper myDbHelper = new DataBaseHelper(m_context);
if (myDbHelper != null) {
myDbHelper.openDataBase();
}
Cursor cursor = myDbHelper.fetchDataFromDataBase(squery);