Android doesn't do file output the same way a desktop does(it's more secure) which is why SavedState doesn't work unless you specify it to save your files to an SD-Card.
I implemented SavedState in my code to test out an AndroidMuffin class I made (just for you lol) and it's working fine for me. You need to add the following to your Slick-AE sources:
In the package org.newdawn.slick.muffin, you need to add a class called AndroidMuffin. The contents are displayed below:
Code:
package org.newdawn.slick.muffin;
import android.content.Context;
import android.os.Environment;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import org.newdawn.slick.util.Log;
/**
*
* @author mangelok
*/
public class AndroidMuffin implements Muffin {
public static Context app;
@Override
public void saveFile(HashMap data, String fileName) throws IOException {
String userHome = Environment.getDataDirectory().getPath();
File file = new File(userHome);
file = new File(file, ".java");
if (!file.exists()) {
file.mkdir();
}
file = new File(file, fileName);
FileOutputStream fos = app.openFileOutput(file.getName(), Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// save hashMap
oos.writeObject(data);
oos.close();
}
@Override
public HashMap loadFile(String fileName) throws IOException {
HashMap hashMap = new HashMap();
FileInputStream fis = null;
try {
fis = app.openFileInput(fileName);
} catch (FileNotFoundException fe) {
Log.error("SavedState files not found!");
return hashMap;
}
try {
ObjectInputStream ois = new ObjectInputStream(fis);
hashMap = (HashMap) ois.readObject();
ois.close();
} catch (EOFException e) {
// End of the file reached, do nothing
} catch (ClassNotFoundException e) {
Log.error(e);
throw new IOException("Failed to pull state from store - class not found");
}
return hashMap;
}
}
In the SlickActivity class you need to add
Code:
org.newdawn.slick.muffin.AndroidMuffin.app = this.getApplicationContext();
to the beginning of your onCreate method (just after super.onCreate(sis))
and lastly, in the SavedState class, the constructor looks like this:
Code:
public SavedState(String fileName) throws SlickException {
this.fileName = fileName;
if (isAndroidAvailable()){
muffin = new AndroidMuffin();
}
else if (isWebstartAvailable()) {
muffin = new WebstartMuffin();
}
else {
muffin = new FileMuffin();
}
try {
load();
} catch (IOException e) {
throw new SlickException("Failed to load state on startup",e);
}
}
and you need to add the method isAndroidAvailable() which looks like this:
Code:
private boolean isAndroidAvailable() {
//my pathetic android checking method
try {
Class.forName("android.app.Activity");
Log.info("Android detected using Muffins");
} catch (Exception e) {
Log.info("Using Local File System");
return false;
}
return true;
}
Hope this helps and is not too confusing!