Reputation: 1991
I am writing a program that is a music player and a lyrics finder. As of now, each time the program loads it has to read each music file and retrieve the ID3 tag to find the artist name, song name, etc. When reading more than a couple files, the process to extract the ID3 tags is very time-consuming. I want to be able to save the ID3 information to my program's private data directory instead of reading each ID3 tag each time the program is ran.
I am confused on serialization and saving/loading something like this. I am guessing I should be using FileOutput/InputStreams or ObjectOutput/InputStreams to achieve this, but I cannot grasp them. Can someone help me out and show me how to save and load my "Song" class correctly? Below are the variables that I need to be able to save.
public class Song implements Serializable
{
private int index = 0, trackLength = 0;
private Bitmap albumArtwork = null;
private String toString, name, trackNumber, artist, album, lyrics, fileName, releaseDate;
}
As you can see, I need to keep track of some Strings, two ints, and a Bitmap. Can someone show me the setup of how to save an array or ArrayList of these to a file, say, named "music_info.sav" or something along those lines? Also, how can you check if this file exists (and if it does not, create the file as empty)?
Upvotes: 2
Views: 5104
Reputation: 24910
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("song.ser"));
out.writeObject(yourArrayList);
out.flush();
out.close();
to write
ObjectInputStream in = new ObjectInputStream(new FileInputStream("song.ser"));
ArrayList<Song> myArrayList = (ArrayList<Song>) in.readObject();
in.close();
to read.
EDIT
I'm not sure about the Bitmap object. You might have to roll your own readObject() and writeObject() method.
private void writeObject(java.io.ObjectOutputStream out)
private void readObject(java.io.ObjectInputStream in)
Here is a link on serializing and de-serializing bitmaps.
http://www.johnwordsworth.com/2011/06/serializing-and-de-serializing-android-graphics-bitmap/
Upvotes: 3
Reputation: 1086
Try using a SQLLite database to store this information instead of rolling your own File serialization. See http://developer.android.com/guide/topics/data/data-storage.html and see if you can make that works.
Upvotes: 2