Search code examples
javaandroidlibgdx

Android app(java): can't read a file and no error message appears


My problem might not be exactly that I can´t read a file but a class that should recieve the info of that file doesn't have that info. I am trying to make a game with libGdx using tile maps. I wanted to create a custom tile map that used 4 ArrayList<ArrayList<Integer>> (one for each quadrant) in order to create a base structure for my maps. These are only used for the game to know that to draw and where (I only draw what is on the screen). The map works fine: I can edit it, save and load using keyboard input. On desktop it works fine but on my android phone the map is empty (I used System.out.println(map.tilesQ1); and got []). I get my textures using:

new Texture("textures/" + int + ".png");

and in also works on android but I have no idea why it can't get "maps/map1". I saved the file with and without a file extension and tried different commbinations of prefixes but nothing seems to work. My smartphone is an Moto G 3 with Lineage OS so I have access to root. I use root explorer and I know the file I am looking for is there inside the "...apk/assets/maps/".

public TileMapHandler(){
}

public static boolean saveMap(String fileName, TileMap map){
    FileOutputStream fos = null;
    ObjectOutputStream out = null;

    Object[] mapArrays = {map.tilesQ1, map.tilesQ2, map.tilesQ3, map.tilesQ4};
    try{
        fos = new FileOutputStream(fileName);
        out = new ObjectOutputStream(fos);
        out.writeObject(mapArrays);
        out.close();
        fos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
    saveFile("/maps/lastSave", "fileName");
    return true;
}

public static void loadMap(String fileName, TileMap map){
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try{
        if(new File(fileName).exists()) {
            fis = new FileInputStream(fileName);
            in = new ObjectInputStream(fis);
            Object[] mapArrays = (Object[]) in.readObject();
            if(mapArrays[0] instanceof ArrayList)
            map.tilesQ1 = (ArrayList<ArrayList<Integer>>) mapArrays[0];
            map.tilesQ2 = (ArrayList<ArrayList<Integer>>) mapArrays[1];
            map.tilesQ3 = (ArrayList<ArrayList<Integer>>) mapArrays[2];
            map.tilesQ4 = (ArrayList<ArrayList<Integer>>) mapArrays[3];
            saveFile("./maps/lastSave", fileName);
            fis.close();
            in.close();
        }
        else{
            return;
        }
    } catch(Exception ex){
        ex.printStackTrace();
        System.out.println("Load map error");
        return;
    }

    saveFile("./maps/lastLoad", fileName);
    if(new File("./maps/recent").exists()){
        ArrayList<String> list = (ArrayList<String>) loadFile("./maps/recent");

        if(!list.contains(fileName)) {
            list.add(fileName);
            saveFile("./maps/recent", list);
        }
    }
    else{
        ArrayList list = new ArrayList<String>();
        list.add(fileName);
        saveFile("./maps/recent", list);
    }
}

And here is just the beginning of my "level class":

public TestLevel(BaseGame g) {
        super(g);
    }

    @Override
    public void create() {
        map = new TileMap();
        TileMapHandler.loadMap("maps/map1", map);
        System.out.println(map.tilesQ1);

As I said: It works on desktop but doesn´t on my android device and i don't even get an exception.


Solution

  • I wasn't able to follow the sugestions you guys gave me. I am working with two platforms and I can't use android specific methods or classes inside the core package. The solution I found to this problem would be to make the core Launcher recieve an Interface as a parameter and make each platform specific launcher create an instance if that inerface with platform specific code so the app would work on android. Unfortunantly I couldn't make it work. Every time I trie to call a method from the instance of the interface I recie a NullPointerException for calling the interface method(any suggestions? Tutorial: https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code). I tried using libGdx methods (I should have searched about it before) and it worked. https://github.com/libgdx/libgdx/wiki/File-handling Thank you guys.