title: android- Libgdx serialization and deserialization
body: <p>I have a <code>Player</code> class which records a players position and several other variables. Additionally in order to remember these states after the game is closed I attempted to serialize and deserialize my <code>player</code> object. This process works fine for the desktop version of my libGdx project however when I run it with my android device it fails (my coordinates are not saved). Furthermore when I check the "desktop" folder I can see the <code>"player.dat"</code> file however in my android folder its not there... At first I was thinking that maybe its something with the way I was debugging and saved the actual game on my phone and ran it, but even so the coordinates did not save. </p>
  
  <p>Player class:</p>
  
  <pre><code>public class Player implements Serializable {
  
      private static final transient String defaultTexture = "data/libgdx.png";
  
      private static final long serialVersionUID = 1L;
      transient Texture texture;
      Vector2 position;
      String textureLoc;
      transient Sprite sprite;
  
      public Player (Vector2 position, String textureLoc){
          this.position = position;
          this.textureLoc = textureLoc;
      }
  
      public void loadContent() {
          if (Gdx.files.internal(textureLoc).exists())
              texture = new Texture(Gdx.files.internal(textureLoc));
          else
              texture = new Texture(Gdx.files.internal(defaultTexture));
          sprite = new Sprite(texture);
      }
  
      public void update(OrthographicCamera camera) {
  
          if(Gdx.input.isTouched()){
              Vector3 tp = new Vector3();
              tp.set(Gdx.input.getX(), Gdx.input.getY(), 0);
              camera.unproject(tp);
              if(tp.y &gt; sprite.getY()){
                  position.y += 4f;
              }else{
                  position.y -= 4f;
              }
          }           
      }
  
      public void draw(SpriteBatch batch) {
          batch.draw(texture, position.x, position.y);
      }
  
      public static void savePlayer(Player player) throws IOException {
          FileHandle file = Gdx.files.local("player.dat");
          try {
              file.writeBytes(serialize(player), false);
          }
          catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static Player readPlayer() throws IOException, ClassNotFoundException {
          Player player = null;
          FileHandle file = Gdx.files.local("player.dat");
          player = (Player) deserialize(file.readBytes());
          return player;
      }
  
      private static byte[] serialize(Object obj) throws IOException {
          ByteArrayOutputStream b = new ByteArrayOutputStream();
          ObjectOutputStream o = new ObjectOutputStream(b);
          o.writeObject(obj);
          return b.toByteArray();
      }
  
      public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
          ByteArrayInputStream b = new ByteArrayInputStream(bytes);
          ObjectInputStream o = new ObjectInputStream(b);
          return o.readObject();
      }
  
      public Vector2 getPosition(){
          return position;
      }
  
      public void setPosition(Vector2 position){
          this.position = position;
      }
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public class MyGdxGame extends ApplicationAdapter { 
      private OrthographicCamera camera;
      Player player;
      SpriteBatch batch;  
      Vector2 position = new Vector2(50,50);
      boolean x = false;
      boolean y = false;  
  
      BitmapFont font;    
  
      @Override
      public void create () {     
          font = new BitmapFont();
          font.setScale(50f);
  
          camera = new OrthographicCamera();
          batch = new SpriteBatch();
          player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "data/mario.jpg");
          if(Gdx.files.local("player.dat").exists()){
              System.out.println("Player Exists. Reading File ...");
  
              x = true;
              try {
                  player = Player.readPlayer();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) { 
                  e.printStackTrace();
              }           
          }else{
              System.out.println("Player does not exist. Creating new player ...");
              y = true;
  
              player = new Player(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2), "data/mario.jpg");          
          }   
  
          player.loadContent(); 
      }
  
      @Override
      public void render () {
          Gdx.gl.glClearColor(1, 1, 1, 1);
          Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   
          player.update(camera);
          batch.begin();
          if(x){font.draw(batch, "Player Exists. Reading File ...", 20, 20);}
          if(y){font.draw(batch, "Player does not exist. Creating new player ...", 100, 100);}
          player.draw(batch);
          batch.end();        
      }
  
      public void dispose(){
          try {
              Player.savePlayer(player);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>My only guess is that maybe there is some specifics to saving/getting serialized files on android, but even so I followed the libGdx API so it should have worked.  </p>