Create a protected directory in the device’s Storage

Asked

Viewed 89 times

2

Through an application it is possible to create, edit and remove both files and directories, but we know that it is necessary for the user to give permission to read and write depending on the occasion. In order for this permission to be granted, you should normally enter a tag <users-permission /> in the <manifest></manifest>, as an example READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE.

See below for a simple command to create a directory:

File dir = new File(context.getExternalFilesDir(Environment.DIRECTORY_DCIM), filename );
if (!dir.exists()) {
    dir.getParentFile().mkdirs();
}

After all the procedure done, inserting documents and images inside this directory, when accessing a file manager/gallery, whether it is native to the smartphone or not, the user can see these files normally.

I have an application where I would like to view these documents only within it, so the way it is today, the files can also be viewed through the gallery.

You can create a protected directory on Storage of the device? Is there any other way to solve this problem, so that the user can not visualize the files outside of the application, or view only through a password?

  • How do you get the path that uses in new File(path)?

  • @ramaral I put this code only as an example. Do you think it is relevant to ask the question?!

  • That is why it will determine whether the directory is private or not. I will put a response.

  • One simple way is to leave the directory with a dot before the name, example. ". Appx". This keeps the directory hidden, and prevents the gallery and native explorers from finding the directory or files inside. Only some applications that have root permission and the option to view hidden files.

  • Why are you wearing DIRECTORY_DCIM? It makes no sense since it refers to the parent folder of public folders.

  • @ramaral I had put only as an example for you have charged. I am actually creating a directory with name apptal inside /mnt/sdcard/. So it’s really getting like this /mnt/sdcard/apptal

  • You should not use written paths. You have tried using getExternalFilesDir() with for example DIRECTORY_PICTURES? That is how I would check whether what I say in the reply is true and that the documentation is right(it is not always).

Show 2 more comments

1 answer

3

Android devices have two file storage areas: internal(Storage) and external(Sxternal).

Files created in the internal area are always private, only the application that created them can access them.
Use getFilesDir() to get the way.

Regarding the files created in the external area depends on the external area type and the way the path is obtained.

Some devices use part of the internal memory as Sxternal, emulating a sdcard. Even though I don’t have one sdcard it is presented to the API as if it were.

Thus, in relation to Sxternal, there is the following:

  • Sxternal in the sdcard:

    • The files are public and applications with READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions can access them.
    • If the path is obtained by getExternalFilesDir() are not visible from the gallery and will be deleted when the application is uninstalled.
  • Sxternal emulated:

    • Files are private if the path is obtained by getExternalFilesDir().
      Note: At least that’s what I understand from documentation:

      If a Shared Storage device is Emulated (as determined by Environment.isExternalStorageEmulated(File)), it’s Contents are backed by a private user data Partition, which Means there is little Benefit to storing data here Instead of the private Directories returned by getFilesDir(), etc..

You can create a protected directory in the device’s Storage?

One solution is to encrypt the files using the classes Cipher, Cipheroutputstream and Cipherinputstream.

  • @Viana It seems to be the case of mark an answer as accepted. If you have an answer that really helped you, mark it as accepted. If you arrived at the solution yourself, post the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.