Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 991
Abhishek Bhardwaj Avatar asked Dec 23 '16 12:12

Abhishek Bhardwaj


People also ask

Does Android have a secret folder?

Google is adding a new feature to its Files by Google app for Android phones to let users lock and hide private files in an encrypted folder. The new Safe folder feature is aimed at people who, for example, share a phone with other members of the family but need to keep some files private.

How do I hide a folder?

To hide one or more files or folders, select the files or folders, right-click on them, and select Properties. On the General tab on the Properties dialog box, check the Hidden box in the Attributes section.


4 Answers

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.

like image 95
schrej Avatar answered Oct 05 '22 10:10

schrej


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);
like image 41
sushant gosavi Avatar answered Oct 05 '22 09:10

sushant gosavi


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

like image 29
knownUnknown Avatar answered Oct 05 '22 09:10

knownUnknown


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.

like image 22
Amjad Khan Avatar answered Oct 05 '22 09:10

Amjad Khan