如何在照片删除刷新Android的MediaStore [英] How to refresh Android's MediaStore upon photo deletion

查看:1040
本文介绍了如何在照片删除刷新Android的MediaStore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:如何让媒体商店刷新删除的文件其入门

删除在code的照片从外部存储之后,我还是看到了槽已删除的照片在画廊 - 空白照片

After deleting a photo in code from the external storage, I still see a slot for the deleted photo in the gallery - blank photo.

看来,画廊反映了媒体存储和删除的照片在媒体库中找到,直到手机重新启动或一般 - 直到介质重新扫描

It seems that the gallery reflects the media store and the deleted photo is found in the media store until the phone is restarted or generally - until the media is rescanned.

尝试扫描删除的文件的确不会帮助扫描已删除的文件(工作只是为新的或现有的文件): MediaScannerConnection.scanFile(Application.get(),新的String [] {file.getPath()},NULL,NULL)(我试过扫描父文件夹以及)。

Trying to scan the deleted file did not help scanning deleted files (works just for new or existing files): MediaScannerConnection.scanFile(Application.get(), new String[]{file.getPath()}, null, null) (I tried scanning the parent folder as well).

也试过 ACTION_MEDIA_SCANNER_SCAN_FILE 无济于事。例如: Application.get()sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(文件)))

Also tried ACTION_MEDIA_SCANNER_SCAN_FILE to no avail. Example: Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))

发送一个广播接收器重新扫描整个外部存储(从而刷新媒体商店)的伎俩: Application.get()sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.fromFile(环境.getExternalStorageDirectory())))

Sending a broadcast receiver to rescan the entire external storage (thus refreshing the media store)did the trick: Application.get().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(Environment.getExternalStorageDirectory())))

但是,这似乎是Android的,由于 4.4 ,尝试手动发送ACTION_MEDIA_MOUNTED系统广播时,会引发安全异常。见@ CommonsWare的帖子:的http://commonsware.com/blog/2013/11/06/android-4p4-permission-regressions.html

BUT, it seems that Android, as of 4.4, throws a security exception when trying to manually send the ACTION_MEDIA_MOUNTED system broadcast. See @CommonsWare's post: http://commonsware.com/blog/2013/11/06/android-4p4-permission-regressions.html

所以,我坚持与在文件刷新媒体商店无解(/照片/视频/等)删除。

So, I'm stuck with no solution for refreshing the media store upon file(/photo/video/etc.) deletion.

推荐答案

我发现对我的作品在4.4上的Nexus 10以下。

I found the following that works for me in 4.4 on a Nexus 10.

// 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得到,然后将其转换为一个文件。

"refreshFile" is the file I deleted that I get from my String "fPath" and then convert it to a file.

String filePath = fPath;        
File refreshFile = new File(filePath);

这篇关于如何在照片删除刷新Android的MediaStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆