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

时间:2023-01-26 14:38:35

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!

如果不可能的话,有人知道更好的惯例吗?如果需要的话,我可以在单独的问题中提出来。蒂娅!

3 个解决方案

#1


3  

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.

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

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.

您只需提供一个onCreate()来创建数据库,提供onUpgrade()来升级,并在数据库版本(onUpgrade()中)发生更改时增加数据库版本(在onUpgrade()中)),Android将为您处理创建和升级数据库。

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.

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

#2


2  

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:

嘿,我和你有同样的问题。我想复制和资产只有在更新的时候。所以我将sharedPreferences store设置为最后安装的版本,并使用它来比较日期:

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

我向字符串添加一个条目。保存应用程序版本的xml文件:

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

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

然后在主类的onCreate方法上加上if子句:

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:

我在onPause方法的sharedPreferences中添加了新的版本字符串,这样在再次调用onCreate之前,就可以确定地添加该字符串:

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.

注意,如果您的版本高于0.0,那么copyfiles将在第一次打开应用程序时运行,并且只有在以后的版本中增加版本字符串时才会再次运行。

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.

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

#3


0  

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.

对于只读资产,我尝试使用文件时间戳(f.lastModified() / f.setSetModified(时间戳)),并且它没有工作,因为setsetmodified(时间戳)在Android上不起作用。至少,在我使用的2.3.4中。

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

参见https://*.com/a/13996418/755804

#1


3  

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.

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

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.

您只需提供一个onCreate()来创建数据库,提供onUpgrade()来升级,并在数据库版本(onUpgrade()中)发生更改时增加数据库版本(在onUpgrade()中)),Android将为您处理创建和升级数据库。

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.

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

#2


2  

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:

嘿,我和你有同样的问题。我想复制和资产只有在更新的时候。所以我将sharedPreferences store设置为最后安装的版本,并使用它来比较日期:

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

我向字符串添加一个条目。保存应用程序版本的xml文件:

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

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

然后在主类的onCreate方法上加上if子句:

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:

我在onPause方法的sharedPreferences中添加了新的版本字符串,这样在再次调用onCreate之前,就可以确定地添加该字符串:

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.

注意,如果您的版本高于0.0,那么copyfiles将在第一次打开应用程序时运行,并且只有在以后的版本中增加版本字符串时才会再次运行。

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.

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

#3


0  

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.

对于只读资产,我尝试使用文件时间戳(f.lastModified() / f.setSetModified(时间戳)),并且它没有工作,因为setsetmodified(时间戳)在Android上不起作用。至少,在我使用的2.3.4中。

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

参见https://*.com/a/13996418/755804