Search code examples
javaandroidfile-iosd-cardandengine

Saved object not being read from sd card


So in an android game (using andEngine) I'm making, I'm trying to save a TMXtiled map as an object and recall it later to save loading time. I have managed to get the files on the sd card but am having trouble trying to read from the sd card. This is the code I used to write the file:

File root = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(root + "/fileTest");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tmxLoader.loadFromAsset(mContext, "tmx/test.tmx"));
oos.close();

Then I use this to read the file from the sd card:

FileInputStream in = new FileInputStream(root + "/fileTest"); 
ObjectInputStream os = new ObjectInputStream(in); 
mTMXTiledMap = (TMXTiledMap) os.readObject();
os.close();

The error I keep getting is a null pointer exception, which is what led me to believe that the file is not being read. I tested this method in java before using it in android and it works, however I was using strings. Is there something wrong with the code above? Also is there a better method for storing these large objects? Another thing I noticed was that the file on the sd card was around 3KB, but I have seen the TiledMap take a lot more space by using the DDMS, so does that mean the files are not being completely written?


Solution

  • ya i think you hadnt given the full path ie with the extension as

    FileInputStream in = new FileInputStream(root + "/test.tmx");
    

    hope this will solve your issue