0

我有一个名为 weatherdata.xml 的 xml(它位于我的 Eclipse 的 >assets< 文件夹中),它看起来像

<?xml version="1.0" encoding="utf-8"?>
<Weather>
<weatherdata>
    <id>1</id>
    <city>Berlin</city>
    <tempc>0°C</tempc>
    <tempf>32°F</tempf>
    <condition>Snowing</condition>
    <windspeed>5 kmph</windspeed>
    <icon>snowing</icon>
</weatherdata>
    <weatherdata>
    <id>5</id>
    <city>Sydney</city>
    <tempc>32°C</tempc>
    <tempf>89.6°F</tempf>
    <condition>Sunny</condition>
    <windspeed>10 kmph</windspeed>
    <icon>sunny</icon>
</weatherdata>

所以我试图在运行时再添加一个城市..我尝试单独使用java并且工作正常

我认为它可以在 android 上正常工作,但由于 android 的功能与桌面应用程序完全不同,所以无法更进一步

我发现很有趣,(虽然它没有附加)

所以我的问题是

  • 这个sdcard是什么,我需要写吗

  • 所以如果我写到 sdcard 我可以在模拟器和实际设备上期望相同的输出

  • 如果是的话,这会是路径吗?/sdcard/weatherdata.xml

除此之外,是否有任何权利(manifest.xml)打扰我写入 xml 文件

4

3 回答 3

1

资产是作为 APK 的一部分打包的只读文件。你不能改变它们。

您有 2 个地方可以为您的应用程序编写文件:

  1. SD 卡(或“外部存储”)
  2. 手机内存中应用程序的私有数据区域

这些有点不同,并且具有不同的优点/缺点,如下所示:

  1. SD卡:

    • 卸载应用时不会删除您在 SD 卡上写入的文件
    • 您需要READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限才能访问 SD 卡上的文件
    • 某些设备不支持 SD 卡,或者用户可能没有在插槽中安装 SD 卡
    • 用户可以删除/替换/格式化 SD 卡,在这种情况下,您在那里写入的任何文件都会丢失
    • 用户可以通过将他的设备连接到 PC 或移除 SD 卡并将其安装在他的 PC 上来读取/写入 SD 卡上的任何文件
  2. 私有数据区:

    • 当您的应用程序被卸载时,您在此处写入的文件将被删除
    • 用户无法读取或写入这些文件(除非他的手机已root)
    • 您不需要任何权限即可在私有数据区域中读取/写入

我建议您将 XML 文件从资产复制到私有数据区域(如果它不存在),然后始终使用私有数据区域中的副本。如果要向其中添加条目,则更新私有数据区域中的副本。

要在私有数据区打开文件,请使用openFileInput()openFileOutput()。模拟器上的行为与设备上的行为相同。

于 2013-05-14T10:06:17.523 回答
0

1) sdcard 是您编写文件的地方(请注意,您在 eclipse 的 assets 文件夹中的 xml 不会存在,它将与您的应用程序捆绑在一起)

2)是的,模拟器和物理设备上的行为应该相同

3) 您可以为文件选择所需的名称,但请注意,它与您放入资产中的文件不同(参见 1)。此外,您应该使用Environment.getExternalStorageDirectory();而不是硬编码目录。

您需要将其添加到您的清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-05-14T10:01:40.323 回答
0

这就是我得到的

 try {
        File file = getBaseContext().getFileStreamPath("weather.xml");
        if (file.exists()) 
        {
            Log.d("weather.xml", "File Found !!!");
            FileOutputStream fOut = getApplicationContext().openFileOutput(
                    "weather.xml", MODE_APPEND);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            String fileContent = "<Weather><weatherdata><city>Versace</city></weatherdata></Weather>"; 
            osw.write(fileContent);
            osw.flush();
            osw.close();
            Log.d("write mode: ", "MODE_APPEND");
        }
        else
        {
                FileOutputStream fOut = getApplicationContext().openFileOutput(
                "weather.xml", MODE_PRIVATE);
                OutputStreamWriter osw = new OutputStreamWriter(fOut);
                String fileContent = "<?xml version="1.0" encoding="utf-8"?><Weather><weatherdata><city>Berlin</city></weatherdata></Weather>"; 

                osw.write(fileContent);
                osw.flush();
                osw.close();
                Log.d("write mode: ", "MODE_PRIVATE");
        }
        Log.d("status", "Finished writing to XML");
    } catch (FileNotFoundException e) { Log.e("Error", "XML writing exception");
    } catch (IOException e) { e.printStackTrace(); }

这里的兴趣点是写入模式,MODE_APPEND - 用于附加一个已经存在的 xml 文件,而 MODE_PRIVATE,用于创建一个新文件并写入它。最后是getBaseContext().getFileStreamPath(),用来检查目标文件是否存在..

于 2013-05-14T16:10:35.770 回答