Android MediaScanner: Remove image file(s) from gallery
Asked Answered
F

2

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.

Fudge answered 8/4, 2014 at 17:38 Comment(4)
Have you ever found a solution to that problem?Bish
Did you find a solution to it? @Bish or did you?Inherit
Please share your answer if you found the solution, eagerly waiting for it..Inherit
I just instead of using. nomedia file cut the file from its original place and rename it with some other self defined extension and when want show it again just chane extension. If eg my picture be pic1.jpg i save it as pic1.aaa this way gallery cant find itSchwartz
Y
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)

Yestreen answered 26/4, 2016 at 9:54 Comment(0)
A
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.

Aldrich answered 5/10, 2017 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.