2012-06-27 51 views

回答

8

在數據庫中存儲視頻文件是不好的做法不方便的方式。因此請將您的視頻文件存儲在外部存儲器內部存儲器中,並將這些視頻文件的路徑存儲在數據庫中。所以你可以從數據庫中快速訪問你的數據。它對你的應用程序性能也很好。

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); 
    
相關問題