系统: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

相关文章:

  • 2022-12-23
  • 2021-05-05
  • 2022-12-23
  • 2021-05-13
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-06-17
  • 2021-11-15
相关资源
相似解决方案