[英]adding file to be read and write in javafxports on android
i want to have some configuration file on my JavaFXPorts Android and iOS. 我想在JavaFXPorts Android和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.
在我的概念证明中,我有XML文件,并且可以读写。 It working perfectly when i deploy it to java jar.
当我将其部署到Java jar时,它可以正常工作。 But on android it seems the xml file can't be found.
但是在android上似乎找不到xml文件。
Here a repo for these proof of concept. 这里是这些概念证明的回购。 https://bitbucket.org/bluetonic/xmlreadwritefx Feel free to check it :)
https://bitbucket.org/bluetonic/xmlreadwritefx随时检查:)
Best Regards, 最好的祝福,
Ivan 伊万
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 ): 对于Android,您的apk安装在某个路径中,并且您可以使用
Context.getFilesDir()
( link )检索可以在其中存储文件的本地路径:
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
返回到使用openFileOutput(String,int)创建的文件存储在文件系统上目录的绝对路径。
In order to access the context from your JavaFX thread you need to use: 为了从JavaFX线程访问上下文,您需要使用:
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: 例如,您可以在Android / Java软件包上创建此帮助程序类:
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. 现在,如果您在Android上运行,则可以调用此类的实例,而如果您在桌面上运行,则仍可以使用标准方法。
Check this blog post to find out how, by using a PlatformProvider
. 通过使用
PlatformProvider
,检查此博客文章以了解如何进行。 I use it to read and write property files, but I don't see any difference for XML files. 我用它来读写属性文件,但是我看不出XML文件有什么区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.