ThatHybrid
ThatHybrid

Reputation: 383

Android serialization issue

I am trying to write object data to a file (how it's done in a standard java program) in an android program and am running in to some issues. Here's the code:

public static final String storeDir = "Adata"; 
public static final String storeFile = "albums";


public static void write(ArrayList<Album> albums) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(storeDir + File.separator + storeFile));
    oos.writeObject(albums);
}

public static ArrayList<Album> read() throws IOException, ClassNotFoundException{
    ObjectInputStream ois = new ObjectInputStream( new FileInputStream(storeDir + File.separator + storeFile));

    return (ArrayList<Album>)ois.readObject();
}

At startup the app crashes and says, "java.io.FileNotFoundException: Adata/albums (No such file or directory)

The folder Adata folder is in the project folder at the same point as the src. Any help is appreciated. Thanks.

Upvotes: 2

Views: 82

Answers (1)

Ads
Ads

Reputation: 6691

I assume that you want in to store in the external directory

Replace your storedir and storeFile as

public static final String storeDir = = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String storeFile = "Adata/albums";

Also you may need to provide permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

To get a better understanding, take a look at Developer site.

Upvotes: 1

Related Questions