开发者

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).

If that's not possible, anyone know of a better con开发者_如何学Govention? I can post that in a separate question if needed. 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.

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.


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜