Abhishek Bhardwaj
Abhishek Bhardwaj

Reputation: 1204

How to create hidden directory in android?

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.

Upvotes: 7

Views: 10866

Answers (4)

schrej
schrej

Reputation: 450

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.

Upvotes: 5

Sushant Gosavi
Sushant Gosavi

Reputation: 3845

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

Upvotes: 3

Amjad Khan
Amjad Khan

Reputation: 1317

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.

Upvotes: 1

knownUnknown
knownUnknown

Reputation: 909

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

Upvotes: 2

Related Questions