首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

如何使用sqlite的blob存储.mp3

2012-03-26 
怎么使用sqlite的blob存储.mp3急,,,,,[解决办法]package com.misoo.SQ01import java.io.ByteArrayOutputS

怎么使用sqlite的blob存储.mp3
急,,,,,

[解决办法]
package com.misoo.SQ01;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
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.SQLiteOpenHelper;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class ac01 extends Activity implements OnClickListener{
private static final String DB_NAME = "mp3Song.db";
private static final int DB_VERSION = 2;
private Button btn, btn2, btn3;
private Cursor cur;
private MediaPlayer mPlayer; 

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

btn = new Button(this);
btn.setId(101);
btn.setText("play");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(this);
LinearLayout.LayoutParams param 
= new LinearLayout.LayoutParams(80, 50);
param.topMargin = 10;
layout.addView(btn, param);

btn2 = new Button(this);
btn2.setId(102);
btn2.setText("stop");
btn2.setBackgroundResource(R.drawable.heart);
btn2.setOnClickListener(this);
layout.addView(btn2, param);

btn3 = new Button(this);
btn3.setId(103);
btn3.setText("exit");
btn3.setBackgroundResource(R.drawable.heart);
btn3.setOnClickListener(this);
layout.addView(btn3, param);
setContentView(layout);
setTitle("Saving into SQliteDB...");
//---------------------------------
init();
setTitle("Saved in SQliteDB.");
}
private DatabaseHelper mOpenHelper;
public void init(){
mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
//-----------------------------------
String sql = "drop table mySong";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.e("ERROR", e.toString());
}
//-----------------------------------
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;
}
//--------------------------------- 
SaveOneSong(db,"s01", R.raw.den_li_guing); 
}
public void SaveOneSong(SQLiteDatabase db, String key, int rid){
ContentValues cv = new ContentValues();
cv.put("song_no", key);

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());
 }
}
 
public void play(String cond){
FileOutputStream os = null;
try{
os = openFileOutput("song.mp3", MODE_WORLD_READABLE);
} catch(FileNotFoundException e){
Log.e("ERROR", e.toString());
}
byte[] red_buf;  
//----------------------------------------
mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = {"song_no", "song_mp3" };
cur = db.query("mySong", col, cond, null, null, null, null);
int k =0;
cur.moveToFirst();
try{
while(!cur.isAfterLast()){
red_buf = cur.getBlob(1);
os.write(red_buf);
k++;
cur.moveToNext();
}
os.flush();
os.close();

}catch(Exception e){
Log.e("ERROR", e.toString());
return;
}

String path = "/data/data/com.misoo.SQ01/files/song.mp3";
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(path);
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mPlayer.start();
}
public void onClick(View v) {
switch (v.getId()) {
case 101:
String cond = "song_no='s01'";
play(cond);
break;
case 102:
stop();
break;
case 103:
stop();
finish();
break;
}
}
public void stop() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
}



[解决办法]
放在数据库里面很浪费

热点排行