我将我的音频文件存储到一个 sqlite 数据库(作为 BLOB),现在我想从数据库中获取它并在单击按钮时播放它。 我的数据库表有三个字段:rowID、图像、音频 在我的表格的每一行中,存储了一个图像和一个音频(作为 BLOB)。
我试过这个但对我不起作用:
byte[] byteAudio2 = null;
Cursor cur1 = db.query("audiofromdb_tbl", null, null, null, null, null, null);
cur1.moveToFirst();
byteAudio2 = cur1.getBlob(cur1.getColumnIndex("audio"));
File tempWav = null;
FileOutputStream fos = new FileOutputStream(tempWav);
fos.write(byteAudio2);
fos.close();
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(fos.getFD());
mp.prepare();
mp.start();
最佳答案
尝试这样的事情:
byte[] buffer = new byte[2048];
int size = 0;
ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(byteAudio2);
while ((size = byteArrayStream.read(buffer, 0, buffer.length)) > 0) {
fos.write(buffer, 0, size);
}
[]
关于android - 从 SQLite 数据库表的 BLOB 字段播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11554961/