1

We have a Xamarin Forms App in production that is losing files on some devices. The trigger appears to be an auto-delete feature on Android that happens when devices are bordering full capacity.

We are able to replicate the issue on multiple devices but have not found a variable or storage location that will prevent our app data from being deleted.

We have tried the below Android application variables:

android:allowBackup="false"
android:fullBackupOnly="false"
android:hasFragileUserData="true"
android:manageSpaceActivity=".ActivityOfMyChoice"

We have also tried using 3 different file locations:

public string GetPersonalFilePath(string filename)
{
    var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    return Path.Combine(path, filename);
}
public string GetLocalApplicationDataFilePath(string filename)
{
    var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
    return Path.Combine(path, filename);
}
public string GetExternalFilePath(string filename)
{
    Context context = Android.App.Application.Context;
    var filePath = context.GetExternalFilesDir("");

    if (filePath != null)
        return Path.Combine(filePath.AbsolutePath, filename);

    return null;
}

We have spent a few days searching the web for ideas to try but have had no success thus far.

Any potential solution or ideas would be appreciated.

Neverlyn
  • 31
  • 5
  • 1
    This does not exist. If it was possible, many apps would claim that ALL their data is necessary. An impossible situation to resolve. If user lets you, you can [backup to their cloud drive](https://developer.android.com/guide/topics/data/backup). OR *detect* that the device is near-full, and *inform* the user how to manage storage on their device. See [Android get free space](https://stackoverflow.com/q/8133417/199364). If there is data that is important to not lose, then store it on your server. Somehow associate it with that user. So you can send it back to device if necessary. – ToolmakerSteve Nov 04 '21 at 00:26
  • @ToolmakerSteve thanks for responding, that makes a lot of sense and is good to know. Is there a specific best practice to determine when a device is "near-full"? E.g. should we be trying to detect 80% full, 500MB remaining storage or something else? (We are not sure about the criteria Android devices have to meet before running an auto-delete; currently, it feels a little random or device-specific.) – Neverlyn Nov 04 '21 at 02:40
  • 2
    @Neverlyn You could check this issue about how to get the Storage info on specific platforms. https://github.com/xamarin/Essentials/issues/454#issuecomment-435463865 – Wendy Zang - MSFT Nov 04 '21 at 05:46
  • @WendyZang-MSFT I'm sure that info will be useful for the implementation... but what I am looking for right now is the specific criteria that trigger the android auto-delete; so I can set my checks a little below that. – Neverlyn Nov 04 '21 at 09:52
  • Sadly, I have no idea how the decision is made by Android. – ToolmakerSteve Nov 04 '21 at 23:58
  • @Neverlyn As i know, the files in the location you provided would not be detele until you uninstall the app. Which location do the operation of auto-delete feature which you said? In internal storage, Files will be deleted when the app is uninstalled. For the Private external files, it similar to internal files, these files will be deleted when the app is uninstalled by the user. Public files will not be deleted. – Wendy Zang - MSFT Nov 05 '21 at 08:00
  • @WendyZang-MSFT we have found that when an Android device is near-full the OS will do some housekeeping, this includes deleting internal files from Apps. We know this happens because we have replicated the issue using multiple physical devices from different manufacturers; and different storage locations. It does not delete all file files from Apps, it selects the largest ones; we know this because some of our small files remain after the auto-delete has occurred. (Our larger files are only a few MB but they are targeted.) – Neverlyn Nov 07 '21 at 00:26
  • @Neverlyn Could you make sure the auto-deleted files are in the location you provided? As i know, the system will automatically delete files in specific cache directory as disk space is needed elsewhere on the device. You could check the link below. https://developer.android.com/reference/android/content/Context.html#getCacheDir() – Wendy Zang - MSFT Nov 10 '21 at 07:55

0 Answers0