0

I have a couple asset files stored in the assets folder.

I use AssetManager getAssets and copy those files to an external dir:

in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
String filePath = outFile.getAbsolutePath();

Then I use the file paths to open the files:

    this.mydat = new RandomAccessSteamIn();
    this.mydat.setFile(filePath1);
    this.mydat = new RandomAccessFile(filePath2, "r");

to read the binary asset files.

This is my first time deploying an Android app, so I have some questions:

On every app upgrade, I would like to replace the binary files in the assets folder (with newer versions), but keep the file names. Will this be a problem, if every time I detect an app upgrade, I copy the files over again to the external dir?

Is there a better way for achieving this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
live-love
  • 48,840
  • 22
  • 240
  • 204
  • 1
    If you copy the assets every time you start the app then you are safe, if you are copying only once then you need a version control system. Reading the new file is no problem, the problem may is what you do with the data readed. – Marcos Vasconcelos Jan 03 '18 at 20:36

1 Answers1

1

I don't see why it might be bad to re-write the files to external dir again if the files were updated; as a matter of fact I've faced this in multiple use-cases, where the file had to be re-written on the external dir each time an edit has been made to it. So I will suggest one approach I use.

First, in build.gradle, I generate an MD5 hash of the file currently in the assets, and I store it as a resValue string.

Second (this could happen wherever you need it in your app), I read the hash stored in resValue and compare it to the previously stored MD5 hash (I store this in SharedPreferences, and this value represents the hash of the asset file before it was updated to a newer version in your case).

Finally, if the hashes don't match, I run the code for reading the file from AssetManager and re-writing it on external dir.

This way whenever your file in assets has been modified, it would generate a different hash, and it would directly be read and written on the external dir.

riadrifai
  • 1,108
  • 2
  • 13
  • 26