25.使用SQLite的Blob储存.mp3档案
这是一个Android范例,兹说明如下:
Step-1: 首先将.mp3档案放入Project的/res/raw/里,如下:
程序一开始执行,建立一个数据库,含有BLOB字段,如下之指令:
sql = "create table mySong("
+ "song_no text not null, "
+ "song_mp3 blob );";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.e("ERROR", e.toString());
return;
}
Step-2: 从Project的/res/raw/读取*.mp3歌曲,然后分段储存到SQLite的BLOB里,如下之指令:
InputStream is = getResources().openRawResource(rid);
int bufSize = 63*1024;
byte[] buffer = new byte[bufSize];
try {
int size = is.read(buffer);
while(size >= 0){
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
out.write(buffer, 0, size);
out.flush();
out.close();
cv.put("song_mp3", out.toByteArray());
db.insert("mySong", null, cv);
size = is.read(buffer);
}
} catch (IOException e) {
Log.e("ERROR", e.toString());
}
Step-3: 从SQLite的BLOB里,读取歌曲并存入
/data/data/com.misoo.SQ01/files/song.mp3,
如下之指令: