android - 如何在删除照片后刷新 Android 的 MediaStore

标签 android mediastore android-mediascanner

问题:如何使媒体存储刷新其已删除文件的条目?

用代码从外部存储中删除照片后,我仍然在图库中看到已删除照片的插槽 - 空白照片。

图库似乎反射(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/

相关文章:

android - Samsung Galaxy S3 中的选项菜单

java - 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像

java - 将图库中的图像和 Mp3 保存到 Android 内部存储目录

java - 媒体扫描仪连接未更新

javascript - 通过 JavaScript 接口(interface)将图像从 Android 传递到 HTML (WebView)

android - 使用 cursor.getCount() 获取计数或在 SQL 子句上使用 COUNT 执行 rawQuery?

android - MediaScanner ServiceConnectionLeaked

android - 如何仅使用其内容 Uri 强制更新文件的 MediaStore

android - 创建 Intent 并将其命名为当前日期和时间

android - 在 Android 中选择视频的最佳方式是什么