11

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.

AndreaF
  • 11,975
  • 27
  • 102
  • 168

3 Answers3

2

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)

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Quoting from Android docs page you recommended for getExternalFilesDir(null): "There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files." (or delete them...) – gregko Jan 06 '17 at 13:53
0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I prefer to avoid to use internal memory since low end device haven't much internal storage, however what is the private internal storage path? (In addition I'm wondering if is there a way to mask the file format in order to make the file appear like a known document type file) – AndreaF Jun 26 '14 at 03:03
  • activity.getFilesDir() returns it. But it should be in the /data/data/full_app_name directory – Gabe Sechan Jun 26 '14 at 03:19
0

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.

Community
  • 1
  • 1
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • I don't want to write in private directory, and I need that the user can also access to the directory of these files easily so isn't important know if is a physic sd card or the virtual storage sd of device. I simply need to avoid remotion by automatic cleaners. – AndreaF Nov 09 '14 at 10:48
  • FAT 32 formated drives do not support permissions. Moreover, `/path/to/external/storage/whatever/it/is/on/the/device/Android/data/com.package.yourapp/files` is accessible by other apps, so users will be able to access this directory. – ToYonos Nov 09 '14 at 11:22
  • ok, FAT32 doesn't supports permission... however what about metadata used to determine the file format on linux system? These cleaners assume that all files where are stored simply objects are temp files. If change the metadata used to determine the file formats the problem should be solved. – AndreaF Nov 09 '14 at 12:22
  • You could try changing file owner but i don't know if android will let you do it : https://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html#lookup – ToYonos Nov 12 '14 at 08:27