13

I am developing an app which hides image files in stock gallery app. For the same, I am creating a .nomedia file and asking MediaScanner to update.

This technique works for a case where I have to show the hidden files, i.e. delete .nomedia file and scan every file present in that folder to show in gallery. But when I want to hide images, putting a .nomedia file and then asking MediaScanner to scan all files in that folder does not hide those pics from gallery.

I am using following code to scan file

private void refreshPic(String path) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(path);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

Am I doing it the right way? Or does removing files from Media Store needs some other stuff?

Testing device: Nexus 5 (4.4.2)

Thanks.

Ravi
  • 34,851
  • 21
  • 122
  • 183
Kapil Jituri
  • 1,241
  • 14
  • 25

2 Answers2

0

Sending broadcast won't work. Use ContentResolver to update the MediaStore

delete(android.net.Uri, ava.lang.String, ava.lang.String)

update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String)

Blo
  • 11,903
  • 5
  • 45
  • 99
shijin
  • 2,998
  • 1
  • 24
  • 30
0

I added the .nomedia file to our app's root folder, and after a day or so (restart seems to make this happen faster) the files got removed from the user's gallery without any other calls to the media scanner.

Have seen this behaviour on Android 4.4.2 and 6.0.1.

Diederik
  • 5,536
  • 3
  • 44
  • 60