2

Hi I have created a folder in sd card holding a db as below

public Databasehandler(Context context) 
{
    //super(context, DATABASE_NAME, null, DATABASE_VERSION);
    super(context, Environment.getExternalStorageDirectory()+ File.separator + "MyAppFolder"+ "/"+DATABASE_NAME, null, DATABASE_VERSION);
}

Now, I am trying to hide this file so that it is not accesible. I tried the below code.

 final String NOMEDIA_FILE = ".nomedia";

    path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAppFolder" );
    path.mkdirs();

    file= new File(path,NOMEDIA_FILE);
    if (!file.exists()) 
    {
        try 
        {
            file.createNewFile();
            Log.e("NOMEDIA_FILE"," ");
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

But, this is not working. Not sure how to hide the file in SDcard . Please Help. Thanks!

sanjana
  • 641
  • 2
  • 15
  • 36

1 Answers1

0

If you're using Java 7 you can use the new java.nio.file.attribute package like so:

Path path = FileSystems.getDefault().getPath("/j", "sa");
Files.setAttribute(path, "dos:hidden", true);

Or, if you're using an older version of Java and/or want to do it using Runtime, try this:

Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path"); 

you can use it like this

final String NOMEDIA_FILE = ".nomedia";

    path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAppFolder" );
    path.mkdirs();

    file= new File(path,NOMEDIA_FILE);
    Files.setAttribute(path, "dos:hidden", true);

or

 Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r"+path);
    if (!file.exists()) 
    {
        try 
        {
            file.createNewFile();
            Log.e("NOMEDIA_FILE"," ");
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
Sunny
  • 219
  • 1
  • 10
  • I tried the second one and I get , java.io.IOException: Error running exec(). Command: [cmd.exe, /C, attrib, -s, -h, -r/mnt/sdcard/EaseTrailCount] Working Directory: null Environment: null – sanjana Jan 08 '14 at 04:42
  • i think u have not created the directory.So first create dir and then hide it – Sunny Jan 08 '14 at 04:54
  • I have created the directory then using this code. Should I specify the db file path or the path of folder containing the db – sanjana Jan 08 '14 at 05:06
  • http://stackoverflow.com/questions/1999437/how-to-make-a-folder-hidden-using-java – Sunny Jan 08 '14 at 05:34
  • 1
    or try this.Put a file named .nomedia in the folder. – Sunny Jan 08 '14 at 05:36