System: android4.4 and above
Features: Take photos in the app and browse and delete photo operations.
Realize:
1. Take photos, save to path specified
2. Notify the System multimedia database to refresh the data.
The main use of mediascannerconnection, this class to the application provides a new multimedia file to the Multimedia scanning service method, and then write data to the system multimedia database, the reference implementation is as follows:
Public classMediascanner {PrivateMediascannerconnection Mediascanconn =NULL; PrivatePhotosannerclient client =NULL; PrivateString FilePath =NULL; PrivateString FileType =NULL; Private StaticMediascanner mediascanner=NULL; /*** Then call Mediascanner.scanfile ("/sdcard/2.mp3"); * */ PublicMediascanner (Context context) {//Create Musicsannerclient if(Client = =NULL) {Client=Newphotosannerclient (); } if(Mediascanconn = =NULL) {Mediascanconn=Newmediascannerconnection (context, client); } } Public StaticMediascanner Getinstanc (context context) {if(mediascanner==NULL) {Mediascanner=NewMediascanner (context); } returnMediascanner; } Private classPhotosannerclientImplementsmediascannerconnection.mediascannerconnectionclient { Public voidonmediascannerconnected () {if(FilePath! =NULL) {mediascanconn.scanfile (FilePath, FileType); } FilePath=NULL; FileType=NULL; } Public voidonscancompleted (String path, Uri uri) {//TODO auto-generated Method StubMediascanconn.disconnect (); } } /*** Scan file tag information * *@paramfilePath * File path Eg:/sdcard/mediaplayer/dahai.mp3 *@paramfileType * File type Eg:audio/mp3 media/* Application/ogg **/ Public voidscanfile (String filepath, string fileType) { This. FilePath =filepath; This. FileType =FileType; //Call Musicsannerclient's onmediascannerconnected () method after connectionMediascanconn.connect (); } PublicString GetFilePath () {returnFilePath; } Public voidSetFilePath (String filePath) { This. FilePath =FilePath; } PublicString Getfiletype () {returnFileType; } Public voidSetfiletype (String fileType) { This. FileType =FileType; }}
View Code
3. Delete the photo and delete the relevant content from the multimedia database. For the delete operation, you can perform the deletion through the content provider direct operation Multimedia database, the reference code is as follows:
if(File.isfile ()) {String FilePath=File.getpath (); if(Filepath.endswith (". mp4")){ intres =context.getcontentresolver (). Delete (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Mediastore.audi O.media.data+ "= \" "+ filepath+" \ "", NULL); if(res>0) {file.delete (); }Else{log.e (TAG,"Delete file Failed"); } }Else if(Filepath.endswith (". jpg") | | Filepath.endswith (". png") | | Filepath.endswith (". bmp")){ intres =context.getcontentresolver (). Delete (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Mediastore . Audio.Media.DATA+ "= \" "+ filepath+" \ "", NULL); if(res>0) {file.delete (); }Else{log.e (TAG,"Delete file Failed"); } }Else{file.delete (); } //Delete data from a multimedia database return; }
View Code
Android App Refresh system Multimedia Library (add or delete multimedia files)