10

I want to create .nomedia file in the internal cache directory where I will store images and tried the following..

File dir = getCacheDir();
File output = new File(dir, ".nomedia");
boolean fileCreated = output.createNewFile();

When I try this fileCreated is false and the file doesn't get created. If I used nomedia without a dot the file gets created but this is no use as the MediaScanner is looking if the .nomedia file exists.

I also tried this with a FileOutputStream and to write data in the file in case it was because I was only created an empty file but this doesn't work either.

Anyone have an idea why this could be failing?

Azalar
  • 187
  • 3
  • 9

1 Answers1

9

Hmm, are you really sure that the file not already exists? Please note that you need to do an ls -a to see a file starting with a dot.

The documentation of File.createNewFile() says:

Returns true if the file has been created, false if it already exists.

If the file is not created for other reasons (i.e. security), it should throw an exception.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Christian Gawron
  • 852
  • 1
  • 6
  • 18
  • Yes the file is being created, it shows up with a ls -a, it doesn't show in File Explorer in DDMS though so I thought it hadn't been created, thanks. – Azalar Feb 13 '11 at 18:44
  • is it really useful to create this file in the internal cache folder? Aren't the files there already non-public for all apps? – android developer Apr 03 '15 at 00:22