Search code examples
androidlibgdxandroid-preferencesobject-object-mapping

Saving texture with Preferences


So what Im trying to achive is: When the user purchases a new item (a texture) they can click a button to replace the old Item(texture) with the new one. What got in my mind first was Objectmaps. So I created this:

ObjectMap<Integer, Texture> screenMap = new ObjectMap<Integer, Texture>();

Im only using two items at the moment just to get a hang of it:

screenMap.put(prefs.getInteger("stoneOne", 0), sdStone);

screenMap.put(prefs.getInteger("stoneTwo", 1), stone_3);

Here´s the methods I use to change the texture:

public void setStone1() {
    stoneImage = new Image(screenMap.get(0));

}

public void setStone(int screenId) {
    stoneImage = new Image(screenMap.get(screenId));

  }

And now to the part I can figure out:

Preferences prefs = Gdx.app.getPreferences("preferences");
                    prefs.putString("textField", textField.getText());
                    prefs.putString("textArea", textArea.getText());
                    prefs.getInteger("stone", );
                    prefs.putInteger("stone", );
                    prefs.flush(); 

I have no Idea what Integer to put in there as you can see I even left it out right now. I tried the screenId integer but it´s not reachable since it is in a void? P.S never mind the weird names I got for things. I took some code from an old project.


Solution

  • Try to walk for another way.

    1. Save Texture to sdcard.
    2. Save path to file into preferences.

    Preferences prefs = Gdx.app.getPreferences("preferences");
                            prefs.putString("texture_one_path", "/sdcard/tex1.jpeg");
                            prefs.putString("texture_two_path", "/sdcard/tex2.jpeg");