How to prevent data deletion on external storage during reinstall / upgrade?

I notice that my application data on external storage (ie SD card) is deleted during "adb install -r". Although this is normal for uninstalling (and then install optionally afterwards to notice this), I don’t understand why this happens in case of reinstallation (and therefore for market updates). I could swear this is not always the case.

Referring to http://developer.android.com/guide/topics/data/data-storage.html#filesExternal I specifically use "Access files on external storage" on Android 2.2, but not "Saving files, which should be shared "or" Saving cache files. " Therefore, I write and read data in the files "/ sdcard / Android / data /// somefolder / data". My preferences are sticking.

@Commonsware: the problem is not so much in getExternalFilesDir () IMHO, as I see that my data is written where I expect. He just doesn't stick. I am using ao:

public static File getDefaultDirectory(Context context, String packageName) {
    File dir;
    if(mActivity_getExternalFilesDir!=null){//API >=8
        dir = invokeGetExternalFilesDir(context, "fortune");
    }else if(mEnvironment_getExternalStorageDirectory!=null){//API <=7
        dir = invokeGetExternalStorageDirectory();
        dir = new File(dir.getPath() + "/Android/data/" + packageName + "/files/");
    }else{
        //should never occur
        return null;
    }
    return dir;
}
+3
source share
1 answer

IIRC, in Android 2.2 there is a bug that causes these symptoms. I recommend using getExternalFilesDir()up to gingerbread.

+5

Source: https://habr.com/ru/post/1766644/


All Articles