Android文件管理器与media数据库的同步问题
发布日期:2021-07-19 12:29:46 浏览次数:12 分类:技术文章

本文共 2519 字,大约阅读时间需要 8 分钟。

Bug Description:
当在文件管理器中修改多媒体文件(包含音乐、视频、图片)后,音乐播放器、视频播放器、gallery app中显示被修改的文件,且打开失败。Android Recorder(录音机)也出现相同问题。
Root Cause:
Android系统自带了一个media数据库,每次开机完成后,系统会自动扫描SD卡和系统并将音乐、视频、图片三类多媒体文件存放到media数据库中对应的表中。当打开对应APP时,APP会从media数据库中查询对应的文件,并显示给用户。此问题原因在于,文件文件管理器修改的文件没有同步到media数据库中,导致APP不能得到最新的文件数据。
Solution:
文件管理器每次修改多媒体文件时,都要将其修改同步到数据库中。

同步方法:

1.  添加文件
当添加一个文件后,可发送ACTION_MEDIA_SCANNER_SCAN_FIL的广播,MediaProvider捕获到这个广播后,就会扫描添加的文件并将其添加到数据库中。
参考代码:
private void notyfyMediaAdd(File file){


                if(file.isDirectory()) {

                        File[] children = file.listFiles();
                        for (File child : children) {

                                notyfyMediaAdd(child);
                        }
                        return;
                }
                
                String type = "";
                type = FileUtilMsg.getFileMimeType(file);
                
                if(type.startsWith("audio/")
                                || type.startsWith("video/")
                                || type.startsWith("image/")
                                || type.equals("application/ogg")
                                || type.equals("application/x-ogg")
                                || type.equals("application/itunes")) {

                        String uriStr = file.getAbsolutePath().replaceFirst(".*/?sdcard", "file:///mnt/sdcard");
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(uriStr)));
                }
        }


2.        删除文件
直接调用MediaProvider的delete函数进行删除。
参考代码:
private void notifyMediaRemove(File file){

                String type = "", where = "";
                type = FileUtilMsg.getFileMimeType(file);
                
                String path = file.getAbsolutePath().replaceFirst(".*/?sdcard", "/mnt/sdcard");
                
                if(type.equals("")) {

                        where = MediaStore.Audio.Media.DATA + " LIKE '" + path + "%'"; 
                        getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                        where = MediaStore.Video.Media.DATA + " LIKE '" + path + "%'"; 
                        getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                        where = MediaStore.Images.Media.DATA + " LIKE '" + path + "%'"; 
                        getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                        return;
                }
                
                if(type.startsWith("audio/")
                                || type.equals("application/ogg")
                                || type.equals("application/x-ogg")
                                || type.equals("application/itunes")) {

                        where = MediaStore.Audio.Media.DATA + "='" + path + "'"; 
                        getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                } else if(type.startsWith("video/")) {

                        where = MediaStore.Video.Media.DATA + "='" + path + "'"; 
                        getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                } else if(type.startsWith("image/")) {

                        where = MediaStore.Images.Media.DATA + "='" + path + "'"; 
                        getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, where, null);
                        
                }
                
        }

3.        更新文件:移动、重命名  
可以先删除原文件,再添加新文件来进行更新。
参考代码:
  调用上述删除和添加的函数。
也可以直接调用MediaProvider的update函数进行更新;

转载地址:https://blog.csdn.net/jdsjlzx/article/details/9080209 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MediaStore 与Media.EXTERNAL_CONTENT_URI
下一篇:java 可重启线程及线程池类的设计

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月31日 18时08分32秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章