将 XML 文件保存到正在运行的 Android 项目

发布于 2024-11-30 07:42:56 字数 354 浏览 1 评论 0原文

首先:查看我的问题 在线读取 XML 并存储它(使用 Java)。阅读已批准的答案以及该答案下方的评论。

所以,我的问题是:即使我已经完成了链接问题中描述的过程,并且 .xml 文件保存到我的 Android 应用程序中的 /res/values 文件夹中,但它根本不显示 - 不显示我正在运行应用程序,也没有在关闭应用程序后运行。

有谁知道如何解决此问题,以便当我生成文件时,即使应用程序正在运行,也可以立即读取和使用它?

First: see my question Reading XML online and Storing It (Using Java). Read the approved answers and the comments underneath that answer.

So, what my question here is: even though I've run through the process described in the linked question, and the .xml file saves to the /res/values folder in my Android App, its not showing up at all - not when I'm running the app, nor after I close the app.

Does anyone have any ideas on how to fix this so that when I generate the file, it will be available right away, even while the app is running, to read and use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-12-07 07:42:56

只需使用此代码,

FileOutputStream fOut = null;
try {
    fOut = this.openFileOutput("your xml file name.xml", MODE_PRIVATE);
    try {
        InputStream is = new FileInputStream("your source file");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        fOut.write(buffer);
    } catch(Exception e) {
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
} 

您就可以在 data/data/packagename/file 文件夹 Thnx 中看到您的 xml 文件。希望这会对您有所帮助。

just use this code,

FileOutputStream fOut = null;
try {
    fOut = this.openFileOutput("your xml file name.xml", MODE_PRIVATE);
    try {
        InputStream is = new FileInputStream("your source file");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        fOut.write(buffer);
    } catch(Exception e) {
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
} 

and you can see your xml file at the data/data/packagename/file folder Thnx. Hope this will help you.

司马昭之心 2024-12-07 07:42:56

我不能 100% 确定您是否在 Java 中运行 XML 解析,或者实际上在 Android 应用程序中运行。

如果您在 Java 中运行,请注意您的项目结构不在模拟器中 - .apk 在运行之前已打包并安装。您需要使用 adb 将文件推送到模拟器(或您的 Android 设备)中,然后您的应用才能看到该文件。

如果您在应用程序中访问该文件:
如果您使用 openFileOutput() 等文件访问方法,它将显示在设备上的私有目录中,即 /data/data//files/

但是,如果您使用“new File(”而不是“context” .openFileOutput”,那么该文件就位于您放置的位置。

I'm not 100% sure if you're running the XML parsing in Java or actually in your Android app.

If you're running in Java, be aware that your project structure isn't live in the emulator - the .apk was packaged up and installed before running. You need to use adb to push files into the emulator (or your Android device) before your app can see the file.

If you're accessing the file in the app:
If you use file access methods such as openFileOutput() it will show up in the private directory on the device, which would be /data/data//files/

However, if you're using "new File(" rather than "context.openFileOutput" then the file is wherever you put it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文