【发布时间】:2018-02-05 09:47:13
【问题描述】:
我有一个以 BLOB 格式保存的音频数据表。我想播放音频文件,但从数据库调用方法时遇到问题。
这是我的 DatabaseAccess.java
public List<byte[]> getAudio(long i) {
List<byte[]> list = new ArrayList<>();
File file;
FileOutputStream fos;
byte[] byteaudio = null;
String selectAudio = "SELECT VocabAudio FROM Vocab WHERE VocabTopic =" + i;
Cursor c = database.rawQuery(selectAudio, null);
if(c.moveToFirst())
do{
byteaudio = c.getBlob(c.getColumnIndex("VocabAudio"));
try{
file = File.createTempFile("audio", "audio");
fos = new FileOutputStream(file);
fos.write(byteaudio);
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}while(c.moveToNext());
return list;
}
这是我的活动类,我想在其中调用我的 getAudio() 方法。它说它找不到符号变量文件(来自 Uri.fromFile(file))。
public void startSlides() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
Intent intent = getIntent();
long topicId = intent.getLongExtra("SelectedTopicId", 0);
databaseAccess.open();
List<byte[]> audio = databaseAccess.getAudio(topicId);
mp= MediaPlayer.create(Choice.this,Uri.fromFile(file));
i++;
mp.start();
databaseAccess.close();
if (j == vocab.size()+1) {
timer.cancel();
}
}
});
}
}, 0, 1000;
}
有人知道我应该如何调用 getAudio() 方法吗?谢谢。
【问题讨论】:
-
可怕的实践,用 BLOB 膨胀数据库!!
-
为什么?为什么
File.createTempFile并使用您的 BLOB 内容编写它,而您可以将其保存在某个文件中,而只是将路径保存在您的数据库中?同意 ModularSynth 100%... -
@ModularSynth 对不起。我是安卓新手。有什么建议我应该怎么做?
-
@pskink 有什么建议可以检索我的 byte[] 音频文件列表而不是使用 File.createTempFile?
-
正如@pskink 建议的那样,只需存储文件的路径即可。更清洁、更快捷。
标签: android sqlite audio android-sqlite android-mediaplayer