是否可以从资产文件中获取最后修改日期?

发布于 2024-10-21 05:02:10 字数 185 浏览 7 评论 0原文

奇怪的问题:是否有可能获取资产文件夹中文件的最后修改日期,或者这是毫无意义且不可能的?

我问这个问题是因为我在应用程序启动时将只读数据库复制到数据文件夹中,但宁愿仅在现有文件比资产文件夹中存储的文件旧时才执行复制(或者如果文件不存在)。

如果那不可能,有人知道更好的约定吗?如果需要,我可以将其发布在一个单独的问题中。蒂亚!

Bizarre question: is it possible to get the last modified date of a file in the assets folder, or would that be pointless and impossible?

I ask because I'm copying a read-only database out of there into the data folder on application start up, but would rather only perform the copy if the existing file is older than the one stored in the assets folder (or if the file doesn't exist).

If that's not possible, anyone know of a better convention? I can post that in a separate question if needed. TIA!

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

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

发布评论

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

评论(4

子栖 2024-10-28 05:02:10

数据库有多大/复杂程度?您可能会发现使用 SQLiteOpenHelper 的实例更容易、更灵活 来处理这个问题,因为只需一次调用 getReadableDatabase(),如果需要,它将创建数据库,并调用您的onUpgrade 升级数据库为你。

您所要做的就是提供 onCreate() 创建数据库,提供onUpgrade() 进行升级,并增加数据库版本(在onUpgrade()) 当它发生变化时,Android 将为您处理数据库的创建和升级。

或者,(我还没有尝试过),它看起来像 AssetManager.list() 可以为您提供资产的路径列表,接下来,使用 File (String path) 获取 File 对象对于数据库,最后 File.lastModified( ) 获取修改日期。

How big/complex is the database? You might find it's easier and more flexible to use an instance of SQLiteOpenHelper to handle this since with one call to getReadableDatabase(), it will create if necessary the database, and call your onUpgrade to upgrade the database for you.

All you have to do is provide an onCreate() to create the database, provide onUpgrade() to upgrade, and increment the database version (in onUpgrade()) when it changes and Android will handle creating and upgrading the database for you.

Alternatively, (and I haven't tried this), it looks like AssetManager.list() can provide you a list of paths to your assets, next, use File (String path) to get a File object for the database, and finally File.lastModified() to get the modified date.

老街孤人 2024-10-28 05:02:10

嘿,我也遇到了和你一样的问题。我只想在较新的情况下进行复制和资产转移。因此,我使sharedPreferences存储了最后安装的版本,并用它来比较日期:

我向strings.xml文件添加了一个条目来保存应用程序版本:

<string name="version">0.3</string>

然后我在主类的onCreate方法上放置了一个if子句:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if( Float.parseFloat(getString(R.string.version)) > prefs.getFloat("LastInstalledVersion", (float) 0.0 ) ) {
                copyfiles();
}

并且我将新版本字符串添加到 onPause 方法上的 sharePreferences 中,这样在再次调用 onCreate 之前肯定会添加它:

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("LastInstalledVersion", Float.parseFloat(getString(R.string.version)) );
editor.commit();

可能比在文件本身中使用版本名称更简单。

请注意,如果您的版本高于 0.0,则 copyfiles 将在应用程序第一次打开时运行,并且仅当您在更高版本中增加版本字符串时才会再次运行。

请记住,此示例仅在您要比较单个文件或不关心单个文件版本时才有效。否则,您可以使用多个不同的字符串来存储文件版本。

Hey, I was having the same problem as you. I wanted to copy and asset over only if it was newer. So I made the sharedPreferences store the last version installed, and used that to compare dates:

I add an entry to the strings.xml file to hold the application version:

<string name="version">0.3</string>

Then I put an if clause on the onCreate method of the main class:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if( Float.parseFloat(getString(R.string.version)) > prefs.getFloat("LastInstalledVersion", (float) 0.0 ) ) {
                copyfiles();
}

And I add the new version string to the sharedPreferences on the onPause method, that way it will be added for sure before the onCreate is called again:

SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("LastInstalledVersion", Float.parseFloat(getString(R.string.version)) );
editor.commit();

Probably simpler than using version names in the file itself.

Note that copyfiles will run the first time the application is opened, if you have version above 0.0, and will only run again if you increase the version string in later versions.

Keep in mind this example only works if you have a single file to compare, or don't care about individual file versions. Otherwise you could use several different strings to store file versions.

冷弦 2024-10-28 05:02:10

对于只读资产,我尝试使用文件时间戳(f.lastModified() / f.setSetModified(timestamp)),它不起作用< /em>因为f.setSetModified(timestamp)在Android上不起作用。至少,在我使用的 2.3.4 上。

请参阅https://stackoverflow.com/a/13996418/755804

For a read-only asset I tried to use the file timestamp (f.lastModified() / f.setSetModified(timestamp)), and it did not work becausef.setSetModified(timestamp) does not work on Android. At least, on the 2.3.4 that I used.

See https://stackoverflow.com/a/13996418/755804

芯好空 2024-10-28 05:02:10

我创建了一个构建任务,用于保存所有资源文件的最后修改日期,并将其保存到资源目录中的新文件 lastModified.txt 中。只需将其放在 build.gradle 的底部即可。仔细检查资产目录的路径。

task saveAllAssetDate {
    def assetDirectory = "src/main/assets";
    def text = ""
    fileTree(assetDirectory).visit { FileVisitDetails details ->
        def name = details.file.path;
        name = name.substring(name.indexOf("assets"));
        text += details.getLastModified() + " " + name + "\n"
    }
    file(assetDirectory + "/lastModified.txt").text = "" + text
}
build.dependsOn saveAllAssetDate

I created a build task that saves the last modified dates of all asset files and saves it to an new file lastModified.txt in the asset directory. Just put this at the bottom of your build.gradle. Double check the path of your asset directory.

task saveAllAssetDate {
    def assetDirectory = "src/main/assets";
    def text = ""
    fileTree(assetDirectory).visit { FileVisitDetails details ->
        def name = details.file.path;
        name = name.substring(name.indexOf("assets"));
        text += details.getLastModified() + " " + name + "\n"
    }
    file(assetDirectory + "/lastModified.txt").text = "" + text
}
build.dependsOn saveAllAssetDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文