[英]Android MediaScanner: Remove image file(s) from gallery
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.
为此,我正在创建一个.nomedia文件并要求MediaScanner进行更新。
This technique works for a case where I have to show the hidden files, ie delete .nomedia file and scan every file present in that folder to show in gallery. 这种技术适用于我必须显示隐藏文件的情况,即删除.nomedia文件并扫描该文件夹中的每个文件以显示在库中。 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.
但是当我想要隐藏图像时,放入一个.nomedia文件然后要求MediaScanner扫描该文件夹中的所有文件并不会隐藏图库中的那些图片。
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?
或者从Media Store中删除文件需要其他一些东西吗?
Testing device: Nexus 5 (4.4.2) 测试设备:Nexus 5(4.4.2)
Thanks. 谢谢。
Sending broadcast won't work. 发送广播不起作用。 Use ContentResolver to update the MediaStore
使用ContentResolver更新MediaStore
delete(android.net.Uri, ava.lang.String, ava.lang.String) 删除(android.net.Uri,ava.lang.String,ava.lang.String)
update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String) update(android.net.Uri,android.content.ContentValues,java.lang.String,java.lang.String)
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. 我将
.nomedia
文件添加到我们应用程序的根文件夹中, .nomedia
一天后(重启似乎可以使这种情况更快),文件从用户的库中删除,而没有任何其他调用媒体扫描程序。
Have seen this behaviour on Android 4.4.2 and 6.0.1. 在Android 4.4.2和6.0.1上看到过这种行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.