zoukankan      html  css  js  c++  java
  • android应用刷新系统多媒体库(增加or删除多媒体文件)

    系统:android4.4及其以上

    功能:app中拍照, 并实现浏览、删除照片操作。

    实现:

    1.拍照,存储到指定路径path

    2.通知系统多媒体数据库刷新数据。

    主要使用MediaScannerConnection,该类向应用提供了将新增多媒体文件发送给多媒体扫描服务的方法,进而将数据写入到系统多媒体数据库,参考实现如下:

    public class MediaScanner {
    
        private MediaScannerConnection mediaScanConn = null;
        private PhotoSannerClient client = null;
        private String filePath = null;
        private String fileType = null;
        private static MediaScanner mediaScanner= null;
    
        /**
         * 然后调用MediaScanner.scanFile("/sdcard/2.mp3");
         * */
    
        public MediaScanner(Context context) {
            // 创建MusicSannerClient
            if (client == null) {
                client = new PhotoSannerClient();
            }
            if (mediaScanConn == null) {
                mediaScanConn = new MediaScannerConnection(context, client);
            }
        }
        
        public static MediaScanner getInstanc(Context context){
            if (mediaScanner==null){
                mediaScanner = new MediaScanner(context);
            }
            return mediaScanner;
        }
    
        private class PhotoSannerClient implements
            MediaScannerConnection.MediaScannerConnectionClient {
    
            public void onMediaScannerConnected() {
    
                if (filePath != null) {
                    mediaScanConn.scanFile(filePath, fileType);
                }
    
                filePath = null;
                fileType = null;
            }
    
            public void onScanCompleted(String path, Uri uri) {
                // TODO Auto-generated method stub
                mediaScanConn.disconnect();
            }
    
        }
    
        /**
         * 扫描文件标签信息
         * 
         * @param filePath
         *            文件路径 eg:/sdcard/MediaPlayer/dahai.mp3
         * @param fileType
         *            文件类型 eg: audio/mp3 media/* application/ogg
         * */
    
        public void scanFile(String filepath, String fileType) {
            this.filePath = filepath;
            this.fileType = fileType;
            // 连接之后调用MusicSannerClient的onMediaScannerConnected()方法
            mediaScanConn.connect();
        }
    
        public String getFilePath() {
            return filePath;
        }
    
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
    
        public String getFileType() {
            return fileType;
        }
    
        public void setFileType(String fileType) {
            this.fileType = fileType;
        }
    
    }
    View Code

    3.删除照片, 并删除多媒体数据库中的相关内容。对于删除操作, 都可以通过content provider直接操作多媒体数据库执行删除,参考代码如下:

      if (file.isFile()) { 
                
                String filePath = file.getPath();
                if(filePath.endsWith(".mp4")){
                    int res = context.getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,  
                        MediaStore.Audio.Media.DATA + "= "" + filePath+""",  
                        null); 
                    if (res>0){
                        file.delete(); 
                    }else{
                        Log.e(TAG, "删除文件失败");
                    }
                }else if (filePath.endsWith(".jpg")||filePath.endsWith(".png")||filePath.endsWith(".bmp")){
                    int res = context.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  
                            MediaStore.Audio.Media.DATA + "= "" + filePath+""",  
                            null); 
                    if (res>0){
                        file.delete(); 
                    }else{
                        Log.e(TAG, "删除文件失败");
                    }
                }else{
                    file.delete(); 
                }
                //删除多媒体数据库中的数据
                return; 
            } 
    View Code
  • 相关阅读:
    二分图 洛谷P2055 [ZJOI2009]假期的宿舍
    并查集 洛谷P1640 [SCOI2010]连续攻击游戏
    贪心 洛谷P2870 Best Cow Line, Gold
    贪心 NOIP2013 积木大赛
    快速幂 NOIP2013 转圈游戏
    倍增LCA NOIP2013 货车运输
    树形DP 洛谷P2014 选课
    KMP UVA1328 Period
    动态规划入门 BZOJ 1270 雷涛的小猫
    KMP POJ 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/suxiaoqi/p/6189472.html
Copyright © 2011-2022 走看看