关于数据库BLOB类型的存储和读取

现在项目的需求是,在客户端录入一段音频,然后存储到sqlite数据库中,当有需求的时候就从数据库中读取出来播放。现在本人用的是xutils插件,在创建sqlite数据库的时候,关于文件类的保存只能装换成byte数组,所以,本人的思路就是:

1,先通过MediaRecorder类得到一个音频文件

2,将得到的音频文件转换成byte数据,存到sqlite数据库中

3,从数据库中读取byte数据,把byte数组转换成文件

4,用MediaPlayer读取文件,并播放

下面贴出每一步的代码:

1,录音代码:

             private MediaRecorder mRecorder = null;

             private FileName = Environment.getExternalStorageDirectory().getAbsolutePath();  
             FileName += "/audiorecordtest.3gp"; //文件存储的路径,自己设置

             mRecorder = new MediaRecorder();  
             mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
             mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
             mRecorder.setOutputFile(FileName);  
             mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
             try {  
                 mRecorder.prepare();  
             } catch (IOException e) {  
                 Log.e(LOG_TAG, "prepare() failed" + e.toString());  
             }  
             mRecorder.start();

2,将得到的音频文件转换成byte数据,并存入数据库中

            try {
                byte soundbyte[] = null;
                File file = new File(FileName);
                FileInputStream fileinput = new FileInputStream(file);
                ByteArrayOutputStream byteout = new ByteArrayOutputStream();
                byte b[] = new byte[1024];
                int n ;
                while((n = fileinput.read(b)) != -1){
                    byteout.write(b, 0, n);
                }
                fileinput.close();
                byteout.flush();
                byteout.close();
                soundbyte = byteout.toByteArray();
                

                //将得到的byte数据存储到数据库中
                SoundMessage sou = new SoundMessage();
                sou.setSound(sound);
                db.save(sou);
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Log.e("file", e.toString());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("io", e.toString());
                e.printStackTrace();
            } catch (DbException e) {
                // TODO Auto-generated catch block
                Log.e("db", e.toString());
                e.printStackTrace();
            }

   3,需要播放的时候从数据库中读取byte数据并转换成本地文件

    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+                     File.separator + "a.3gp"; //本地存储的路径,自己设置

            try {
                    FileOutputStream fs = new  FileOutputStream(path);
                    SoundMessage soud = db.findFirst(SoundMessage.class); //从数据库中读取byte数据
                    fs.write(soud.getSound());
                    fs.close();
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    Log.e("file", e1.toString());
                    e1.printStackTrace();
                } catch (DbException e) {
                    // TODO Auto-generated catch block
                    Log.e("db", e.toString());
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.e("IO", e.toString());
                    e.printStackTrace();
                }   

     4,用MediaPlayer读取文件,并播放

                MediaPlayer mPlayer = new MediaPlayer();  
                try{  
                    mPlayer.setDataSource(path);
                    mPlayer.prepare();  
                    mPlayer.start();  
                    timetext.setText(mPlayer.getDuration()+""); //显示音频的时间,(毫秒)
                }catch(IOException e){  
                    Log.e(LOG_TAG,"播放失败"+e.toString()); 
                }
                //播放完之后删除本地文件(本人测试,要是不删除会报错:Unable to create media player)
                File file = new File(path);
                file.delete();

最近开发的项目对数据库的操作比较多,所以一边开发项目一边学习,对数据库的了解也多了一些,本人发现,掌握数据库的技术还有很有用的                        

转载于:https://my.oschina.net/u/2549561/blog/820116

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值