Allinone51 Allinone51 - 1 year ago 81
Android Question

File in Android internal storage not readable after update

I have an application on the google play store. In this application I save some data in a file (database would be better but would mean more work right now). This works perfectly but the problem is that when I'm running version 2 for example and I save data in a file, I can get it back. But if I then update to version 3, data isn't loaded but the file still exist in the applications 'files' directory. This file is saved in the internal storage of the device. Now I don't know why this happens but I read on the internet that another sharedUserId could be the cause, but I don't have any sharedUserId in my manifest. I know that Android will then generate one, but is this one always the same? Even if the app is compiled on another computer?
Can someone help?

Thanks in advance!


Some code I use to read and write to the file:

To write:

try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);

} catch (IOException e) {
LogUtils.e(IoUtils.class.getSimpleName(), e.getMessage());
} finally {


To read:

try {
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is;
is = new ObjectInputStream(fis);
Serializable objectOut = (Serializable) is.readObject();
return objectOut;
} catch (StreamCorruptedException e) {
LogUtils.e(IoUtils.class.getSimpleName(), e.getMessage());
} catch (IOException e) {
LogUtils.e(IoUtils.class.getSimpleName(), e.getMessage());
} catch (ClassNotFoundException e) {
LogUtils.e(IoUtils.class.getSimpleName(), e.getMessage());
return null;

After the update nothing seems to get out of that file but I don't get any error either. With a ROOT file explorer I can go to /data/data/myApp/files/ and there I find the file who has the same size as before the update.

Answer Source

Serializable without any customization, should not be used for long term storage, as the format of the object may change between app versions. I would assume you added one variable to the object in the new version, (or a Library object changed,) and therefore your Serializable is broke.
What you should do is use custom serialization by overwriting functions

private void writeObject( out) throws IOException`

private void readObject( in) throws IOException, ClassNotFoundException;

private void readObjectNoData() throws ObjectStreamException;

  To retrieve the old data, change the serialization to read exactly the old format. You should also put in the future some version identifier to the serialization, so that you can correctly read it.
All this is very messy, and usually you are much better off saving to json (or XML), which will be much more robust.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download