简体   繁体   English

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

[英]Is it possible to get last modified date from an assets file?

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). 我问,因为我在应用程序启动时将一个只读数据库复制到数据文件夹中,但是如果现有文件比存储在assets文件夹中的文件旧(或者如果文件),则只会执行复制不存在)。

If that's not possible, anyone know of a better convention? 如果那不可能,任何人都知道一个更好的约定? I can post that in a separate question if needed. 如果需要,我可以在一个单独的问题中发布。 TIA! TIA!

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()调用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() ),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对象,最后File.lastModified()获取修改日期。

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存储了最后安装的版本,并用它来比较日期:

I add an entry to the strings.xml file to hold the application version: 我在strings.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,则复制文件将在第一次打开应用程序时运行,并且只有在更高版本中增加版本字符串时才会再次运行。

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. 否则,您可以使用几个不同的字符串来存储文件版本。

For a read-only asset I tried to use the file timestamp ( f.lastModified() / f.setSetModified(timestamp) ), and it did not work because f.setSetModified(timestamp) does not work on Android. 对于只读资产,我尝试使用文件时间戳( f.lastModified() / f.setSetModified(timestamp) ),但它不起作用,因为f.setSetModified(timestamp)在Android上不起作用。 At least, on the 2.3.4 that I used. 至少,在我使用的2.3.4上。

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

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. 我创建了一个构建任务,它保存所有资产文件的最后修改日期,并将其保存到资产目录中的新文件lastModified.txt Just put this at the bottom of your build.gradle . 只需将它放在build.gradle的底部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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM