Question

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!

Was it helpful?

Solution

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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top