Home » Android » java – Protect and unprotect file to avoid accidental elimination in Android by bugged cleaning apps

java – Protect and unprotect file to avoid accidental elimination in Android by bugged cleaning apps

Posted by: admin May 14, 2020 Leave a comment

Questions:

In my Android App I save some files with some data file using

FileOutputStream savedList = new FileOutputStream(Path);

in a folder named myApp located in the SD storage

Unfortunately I have noticed that some cleaner Apps, not well implemented, also very popular (e.g. CleanMaster) wrongly remove the files every time the user perform a temp\trash file cleaning causing problems.

Is there a way to protect (and unprotect for writing) the file programmatically to avoid this?

How to change file permissions?

Since aren’t used the file extensions to recognize the file format, how could I change the metadata of the file that are used to determine the file format so that these file are see as documents by these apps? I suppose that the scan of these Cleaners use some strategy based on Linux file format recognition and remove all object files.

How to&Answers:

Android allows to have private directory on SD card for the app.
you can get the path for private directory for your app as follows.

File myDir = getExternalFilesDir(null);

The null parameter indicates that you are going to store any type of files in the directory

myDir.mkdirs();
Log.d("info", myDir.getPath());

These files are internal to the applications, and not typically visible to the user as media.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

  1. Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using getExternalStorageState(File).
  2. There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.

This solution worked for me as cleaning apps on devices don’t clean these folders considering them as private folders for the respective apps.

Checkout following link from android docs. Context.getExternalFilesDir(java.lang.String)

Answer:

Write it to your private internal drive, so they don’t have permission to touch it. SD cards are just FAT32 drives, so they don’t support file permissions or access lists.

On 4.4 phones you may be ok, as Google basically prevents any writes to the SD card outside of a private directory. Cleaner type apps won’t work on it at all, for better or worse.

Answer:

First, you should read the first answer of this question. The thing to remember :

No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be “external storage”. On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash.

The SD card is a vague notion, it’s quite impossible to be 100% sure you are writting on the SD card.

That thing said, you should use the Android API to write your file on the private directory of the app, located in /path/to/external/storage/whatever/it/is/on/the/device/Android/data/com.package.yourapp/files

Use getExternalFilesDir to get the above File and write your file on the private directory of your app, this way, no one will be able to delete it.