android應用重新整理系統多媒體庫(增加or刪除多媒體檔案)

來源:互聯網
上載者:User

標籤:setfile   getpath   method   player   應用   串連   代碼   string   stat   

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

 

android應用重新整理系統多媒體庫(增加or刪除多媒體檔案)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.