I'm creating a android application which is a dialect dictionary. My problem is I don't know how to store a audio.mp3 file in the database.
Can anybody teach me or share some codes of how to make it?
I'm creating a android application which is a dialect dictionary. My problem is I don't know how to store a audio.mp3 file in the database.
Can anybody teach me or share some codes of how to make it?
While you can store it using BLOB data type, but I strongly suggest you to store on sd-card and store it's path in db. This way it will help you run your application faster.
Advantages of storing media type data as file is
Disadvantage of storing media type data in sqlite is
The main reason for storing sound files on database might be product line approach. You can develop many many similar applications by just modifying your database and not touching your code.
I do not comment on application size because there are comments about it and I do not look at it.
Yes. You can store your small sized sound files in sqlite database as blob fields. It works well. First store your data in database then retrieve it and put it in a temp file. That way you can store your sound files in database.
Check this:
//soundData is mp3 byte array taken from sqlite as blob
soundDataFile = File.createTempFile( "sound", "sound" );
FileOutputStream fos = new FileOutputStream( soundDataFile );
fos.write( soundData );
fos.close();
mediaPlayer.reset();
mediaPlayer.setDataSource( soundDataFile.getAbsolutePath() );
mediaPlayer.prepare();
mediaPlayer.start();
As per your requirement, say you have your 5 mp3's are stored on SDCard stored in folder (or directory) "MySongs". So, just scan the folder by,
File[] myFiles;
File pathToMySongs = new File(
Environment.getExternalStorageDirectory() + "/MySongs");
if(pathToMySongs.exists())
{
myFiles = pathToMySongs.listFiles();
}
else
Toast.makeText(getBaseContext(), "No files found.", Toast.LENGTH_SHORT).show();
Till now, you get all the files in that folder. Now, simply add the path to database by using for each loop, like
for(File file : myFiles)
{
String path = file.getAbsolutePath();
// apply your logic of inserting in database here
}
Check this tutorial on developer.android.com: http://developer.android.com/training/basics/data-storage/databases.html
It teaches how to create database and all CRUD operations on database.