7

I am working on an application where I have created some directory which I am accessing through my application I want to make that directory hidden for security purpose .Such that the user can access them only within the application does not access them outside the application as like through file manager. Any help is appreciated. Don't make it duplicate because I search out all the answer, but no one has worked for me.

Abhishek Bhardwaj
  • 1,164
  • 3
  • 14
  • 39

4 Answers4

5

Just appending a dot before the folder name will not protect it. It is only invisible to the user. It can still be accessed from apps, including file managers and therefore the user. It's just hidden by most file managers by default.

As you want to hide the files for security purposes, you should use Android's internal storage.

From the official Android developer guide:

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

Example:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

Android developer guide


You could also encrypt your files and store the encryption key in the android Keystore.

Here is a good answer regarding encryption of files in android.
Official guide regarding the Android Keystore.

Community
  • 1
  • 1
schrej
  • 450
  • 3
  • 10
  • Thanks for your response can I make my file inaccessible when user try to access them outside the application such that the user can't see them outside the application how's that possible ? – Abhishek Bhardwaj Dec 24 '16 at 04:26
  • If you store them in the internal storage with `Context.MODE_PRIVATE` the user shouldn't be able to see the file as he cannot open the directory where it's stored in i think. – schrej Dec 24 '16 at 12:58
3

Be clear "You want to create directory or folder which is not accessible for other application"(Which is your application folder) Or Create Folder any location but it is hide from your

For First Solution is -

public static File saveFileInAppDirectory(Context context,byte[] inpute, String directoryName,String fileName){
        File mypath;
        File directory = new File(context.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), directoryName);
        if (!directory.mkdirs()) {
            directory.mkdir();
        }
        mypath = new File(directory, fileName);
        try {
            InputStream is = new ByteArrayInputStream(inpute);
            FileOutputStream f = new FileOutputStream(mypath);
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();

        } catch (Exception e) {
            Log.e("SAVE_IMAGE", e.getMessage(), e);
            e.printStackTrace();
        }

        return mypath;
    } 

It will create Directory of your app folder Path - Android/Data/Data/Your.Package.Name/FolderName/FileName

For second Solution - just change file name

File mypath = new File(directory, "."+fileName);

If you want to achive both than just replace

new File(directory, fileName); with  new File(directory, "."+fileName);
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
2

just write the directory name followed by a dot(.) example:

.myDir or .myDir1

so these directories will not be visible through file manager. And while accessing these directories call them using dot(.) only example:

"/path/to/folder/.myDir/"

same can be done for filename

knownUnknown
  • 869
  • 11
  • 18
1

For Hiding Folder in Android

Name of your folder is MyApplicationFolder then u need to add (.)Dot in front of the folder name like .MyApplicationFolder.

So When the Folder is created then the folder is hidden mode for images,video,etc inside but it will be visible in FileManager.

Amjad Khan
  • 1,309
  • 15
  • 32