您的位置:首页 > 移动开发 > Android开发

android:使用SQLite的Blob储存.mp3档案

2010-03-26 10:26 513 查看
25.使用SQLiteBlob储存.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,
如下之指令:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: