Is it possible to get last modified date from an assets file?
Asked Answered
G

4

14

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!

Gymnasiast answered 9/3, 2011 at 15:34 Comment(2)
For others who want to know: the answer's no. I myself resorted to using a version naming convention and regular expressions to parse and determine the file's version.Gymnasiast
can u describe a bit more how u did it please?Daggerboard
L
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.

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.

Lyons answered 9/3, 2011 at 16:8 Comment(4)
I'm already extending SQLiteOpenHelper but didn't want to have to include the database version in code or XML if possible- I wanted a way to compare the databases themselves. Alternatively I suppose I could store the version in the databases, but then I'd have to copy the new database to the data folder every time just to open it and see how new it is, so I might as well overwrite the existing in every case.Gymnasiast
Oh, and I'm pretty sure you can't open a File from the AssetManager, I think it has to be an InputStream. But maybe I'm wrong?Gymnasiast
No, actually I think you're right. Unfortunately, it looks like you're going to have to store some kind of flag in a shared preference or the database in order to determine if you need to overwrite the in-use db.Lyons
Cool, I've resorted to using a version naming convention. Thanks for taking the time.Gymnasiast
O
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:

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.

Osugi answered 27/5, 2011 at 12:54 Comment(0)
T
1

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://mcmap.net/q/810796/-file-lastmodified-is-never-what-was-set-with-file-setlastmodified

Tellurate answered 27/12, 2012 at 5:59 Comment(0)
H
0

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
Hulton answered 15/11, 2018 at 2:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.