Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 654
AndreaF Avatar asked Jun 26 '14 02:06

AndreaF


1 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)

like image 165
Sameer Chamankar Avatar answered Oct 04 '22 14:10

Sameer Chamankar