1

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?

Mycoola
  • 1,135
  • 1
  • 8
  • 29
Loki11
  • 13
  • 1
  • 1
  • 4

3 Answers3

5

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

  • Your application's total size get reduce.
  • It runs faster and smoother as using less RAM.

Disadvantage of storing media type data in sqlite is

  • Your total application size will get increase.
  • You will be need more RAM to load such huge application.
  • Since bigger size it takes time in sharring & downloading.
Kitkat
  • 77
  • 16
  • Thanks...but can you give me some examples. – Loki11 Jan 10 '14 at 05:52
  • This kind of implementation is actually required in certain software projects where the number of mp3 (or other types of binary) files are few and finite, and considered as configuration data. For example, 2 or 3 short mp3 files used for alarm sounds, and needs to be transported across application installations manually, along with other configuration data. – C-- Mar 14 '17 at 10:58
1

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();
Or Uc
  • 21
  • 2
0

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.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • I understand the question properly and I'm trying all your comments now. Because I need it so bad. And please understand me if you think that I get it slow coz I'm just a beginner and I don't have a background about android. – Loki11 Jan 10 '14 at 06:46
  • 1
    @Kitkat I answered as per the requirement I understood. You should be able know that user is still a beginner with Android Development. He need proper guidance, with some sample codes and some references. – Chintan Soni Jan 10 '14 at 07:03
  • While you may be right but still I can not see if OP has really asked "How to retrieve images from SD-Card" ? – Kitkat Jan 10 '14 at 07:05
  • 1
    Thank you for your understanding guys...Please give me your guidance. – Loki11 Jan 10 '14 at 07:28