Ivancodescratch
Ivancodescratch

Reputation: 405

adding file to be read and write in javafxports on android

i want to have some configuration file on my JavaFXPorts Android and iOS. But it seems it don't easy as i expected.

Does anyone have any idea to approach the solution?

I've already tried. In my proof of concept, i have XML file and it can be read and wrote. It working perfectly when i deploy it to java jar. But on android it seems the xml file can't be found.

Here a repo for these proof of concept. https://bitbucket.org/bluetonic/xmlreadwritefx Feel free to check it :)

Best Regards,

Ivan

Upvotes: 0

Views: 717

Answers (1)

José Pereda
José Pereda

Reputation: 45456

For Android, your apk is installed in some path, and you can retrieve the local path where your files can be stored with Context.getFilesDir() (link):

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

In order to access the context from your JavaFX thread you need to use:

Context context = FXActivity.getInstance();

And now you can retrieve a file with the above mentioned method.

For instance, you can create this helper class on the Android/Java Package:

import javafxports.android.FXActivity;
import android.content.Context;
import java.io.File;

public class FileManager {
    private final Context context;

    public FileManager(){
        context = FXActivity.getInstance();
    }

    public File getFile(String fileName){
        return new File(context.getFilesDir(), fileName);
    }

}

Now an instance of this class can be called if you're running on Android, while you can still use the standard approach if you are running on desktop.

Check this blog post to find out how, by using a PlatformProvider. I use it to read and write property files, but I don't see any difference for XML files.

Upvotes: 2

Related Questions