问题:如何使媒体存储刷新其已删除文件的条目?
用代码从外部存储中删除照片后,我仍然在图库中看到已删除照片的插槽 - 空白照片。
图库似乎反射(reflect)了媒体存储,并且删除的照片在媒体存储中找到,直到手机重新启动或通常 - 直到重新扫描媒体。
尝试扫描已删除的文件不会有助于扫描已删除的文件(仅适用于新文件或现有文件):MediaScannerConnection.scanFile(Application.get(), new String[]{ file.getPath()}, null, null)
(我也尝试扫描父文件夹)。
还尝试了 ACTION_MEDIA_SCANNER_SCAN_FILE
但无济于事。示例:Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))
发送广播接收器重新扫描整个外部存储(从而刷新媒体存储)就成功了:Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(Environment.getExternalStorageDirectory( ))))
但是,从 4.4 开始,Android 似乎在尝试手动发送 ACTION_MEDIA_MOUNTED 系统广播时抛出安全异常。请参阅@CommonsWare 的帖子:http://commonsware.com/blog/2013/11/06/android-4p4-permission-regressions.html
因此,我无法解决删除文件(/照片/视频/等)时刷新媒体存储的问题。
最佳答案
我发现以下内容适用于 Nexus 10 上的 4.4。
// request scan
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, SELECT_PICTURE);
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(refreshFile));
sendBroadcast(scanIntent);
“refreshFile”是我删除的文件,我从我的字符串“fPath”中获取,然后将其转换为文件。
String filePath = fPath;
File refreshFile = new File(filePath);
关于android - 如何在删除照片后刷新 Android 的 MediaStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19955546/