简体   繁体   中英

Android, how to prevent internal storage files to be deleted when the app is uninstalled

I'm developing an app which stores few setting in two .xml files, saved on internal storage. I need to save them in there, so please don't answer me "Save them on SD-cards".

I try to uninstall an then re-install (from Android Studio) my app to see if the android:allowBackup="true" works also for internal stored file but the answer was no.

Is this because I done the re-install from the IDE or I need to add some code somewhere?

Thanks for help.

You can save those file using Environment.getExternalStorageDirectory() This stores on the external storage device. Dont get confused with the term external storage as the SD card. SD card is the secondary external storage. But Environment.getExternalStorageDirectory() returns top-level directory of the primary external storage of your device which is basically a non removable storage.

So the file path can be /storage/emulated/0/YOURFOLDER/my.xml

So even if you uninstall the app, these files will not get deleted.

You can use this snippet to create a file in your primary external storage:

private final String fileName = "note.txt";    
private void writeFile() {

       File extStore = Environment.getExternalStorageDirectory();
       // ==> /storage/emulated/0/note.txt
       String path = extStore.getAbsolutePath() + "/" + fileName;
       Log.i("ExternalStorageDemo", "Save to: " + path);

       String data = editText.getText().toString();

       try {
           File myFile = new File(path);
           myFile.createNewFile();
           FileOutputStream fOut = new FileOutputStream(myFile);
           OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
           myOutWriter.append(data);
           myOutWriter.close();
           fOut.close();

           Toast.makeText(getApplicationContext(), fileName + " saved", Toast.LENGTH_LONG).show();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

Don't forget to add below permission in Android Manifest

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

You can then read that file as below:

private void readFile() {

       File extStore = Environment.getExternalStorageDirectory();
       // ==> /storage/emulated/0/note.txt
       String path = extStore.getAbsolutePath() + "/" + fileName;
       Log.i("ExternalStorageDemo", "Read file: " + path);

       String s = "";
       String fileContent = "";
       try {
           File myFile = new File(path);
           FileInputStream fIn = new FileInputStream(myFile);
           BufferedReader myReader = new BufferedReader(
                   new InputStreamReader(fIn));

           while ((s = myReader.readLine()) != null) {
               fileContent += s + "\n";
           }
           myReader.close();

           this.textView.setText(fileContent);
       } catch (IOException e) {
           e.printStackTrace();
       }
       Toast.makeText(getApplicationContext(), fileContent, Toast.LENGTH_LONG).show();
   }

Starting API level 29 there is the "hasFragileUserData" manifest flag

The documentation states that

If true the user is prompted to keep the app's data on uninstall. May be a boolean value, such as "true" or "false".

Sample code:

<application
  ....
  android:hasFragileUserData="true">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM