Some users report losing data

392 views Asked by At

After a long time having an app run without any problems - I got a report form a user stating the data / photos saved was lost now and then. I.e. it may work initially, but at a later stage the content appear gone. I can not duplicate the problem, so I am trying to see if I can "guess" it.

First thing to ensure is that I am not saving data in a way that is somehow not feasible with recent Android versions. Hence here is how I currently save data:

...

First try

File storage_files_dir_file = ctx.getExternalFilesDir("");
if (storage_files_dir_file.exists() == false) {
  return false;
}
// ... some code here prepping content
os = new FileOutputStream(path_final,true);
// ... some code here writing content
MediaScannerConnection.scanFile(ctx, new String[] {s}, null, null);
return true

If the above returns false switch to

File storage_files_dir_file = ctx.getFilesDir();
if (storage_files_dir_file.exists() == false) {
  return false;
}
// ... some code here prepping content
os = new FileOutputStream(path_final,true);
// ... some code here writing content
return true

...

My AndroidManifest.xml includes

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

...

If indeed it is a file problem I see the following possibilities:

  • Data access issue?
  • The saved files get removed by Android?
  • 3rd party cleaning app?
  • Related to if running on phones with a SD card in bay?
  • Related to if running on phones with app installed on SD card in bay?
  • Phone running completely out of space?
  • New: Android 8 users deleting data and photos by accident?
  • New: Combination of Android 8 + e.g. either external app data or internal app data.

It is has been a long while since I coded the above - but I recall the ambition was to save the data at as permanent and accessible place as possible for easy backup.

...

My last update to the app was in March 2017.

Update

I am wondering if the culprit of the problem could be Android 8 Oreo. I have not had access to his myself yet, but from articles it seems it is now possible to delete photos across apps in one go? + I have seen screenshots where users can select to delete both app-data and app-cache? If this is the reason: Any difference if using external or internal app-data storage? Or way to prevent?

My app data is not affected by any cleanup app I have tried on Android 6 and Android 7, but I only tried a quite limited amount compared to all the different companies out there producing Android phones.

...

Does anyone know if it possible to get a file deletion log from users they can send? Maybe that would help diagnose the problem.

2

There are 2 answers

1
Grisgram On

Your problem sounds more like an external cleanup of any kind to be honest.

But anyway, maybe you want to take a look at this: We had issues with a file generated too since Android 8, I then came upon this article: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

We changed our file access to be done via such providers and everything is fine since then. Hope this helps, Cheers Gris

1
Amani On

- getExternalFilesDir()

If you have files that are tied to your application that are simply too big to risk putting in internal storage, or if the user should be able to download the files off their device at will, you can use getExternalFilesDir(), available on any activity or other Context. This will give you a File object pointing to an automatically-created directory on external storage, unique for your application. While not secure against other applications, it does have one big advantage: when your application is uninstalled, these files are automatically deleted, just like the ones in the application-local file area.


- getExternalStoragePublicDirectory()

If you have files that belong more to the user than to your app - pictures taken by the camera, downloaded MP3 files, etc. - a better solution is to use getExternalStoragePublicDirectory(), available on the Environment class. This will give you a File object pointing to a directory set aside for a certain type of file, based on the type you pass into getExternalStoragePublicDirectory(). For example, you can ask for DIRECTORY_MOVIES, DIRECTORY_MUSIC, or DIRECTORY_PICTURES for storing MP4, MP3, or JPEG files, respectively. These files will be left behind when your application is uninstalled.

so to prevent data lost when user deletes app data , you can use getExternalStoragePublicDirectory() instead of getExternalFilesDir() If you have files that belong more to the user than to your app