Android App Refresh system Multimedia Library (add or delete multimedia files)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.