Android Add new media to Media Library

Source: Internet
Author: User

In the default case. Media files that are created by your own programs and stored in the application's private folder are not available to other applications. These media files can be used by other applications. They need to be added to the Media Library, and there are two ways to do it now.

The first: Use a media scanner to add to the Media Library.

/*** Scan the files in the specified directory and add to the Media Library *@paramFilePath * Path*/    Private voidScanmedia (FinalString FilePath) {mediascannerconnectionclient Mediasan=Newmediascannerconnectionclient () {PrivateMediascannerconnection MSC =NULL; {                //First you need to create a connection to the media scannerMSC =NewMediascannerconnection (mainactivity. This, This);            Msc.connect (); } @Override Public voidonscancompleted (String path, Uri uri) {//The scan finishes closing the connection to the media scannerMsc.disconnect (); Mainactivity. This. Runonuithread (NewRunnable () {@Override Public voidrun () {Toast.maketext (mainactivity. This, "Scan done! ", Toast.length_short). Show ();            }                }); } @Override Public voidonmediascannerconnected () {//You can specify a MIME type if you do not specify media scaner to assume a type based on the file nameString MimeType =NULL;            Msc.scanfile (FilePath, MimeType);    }        }; }
View Code

The main use here is "mediascannerconnectionclient" and "mediascannerconnection" two classes.

Implementation steps:

(1) Create a connection to the media scanner.

(2) Call the Scanfile method to scan the file.

(3) When the scan is complete, close the connection to the scanner.

Second: Manually add to Media Library

Contentvalues values =Newcontentvalues (); //title of the media fileValues.put (Audio.AudioColumns.TITLE, "My Media files"); //time StampValues.put (Audio.audiocolumns.date_added,system.currenttimemillis ()/1000); //File TypeValues.put (Audio.AudioColumns.MIME_TYPE, "Audio/amr"); //specifies the file path. Must be an absolute pathValues.put (MediaStore.Audio.Media.DATA, "/sdcard/myvideo.mp4"); //Insert the file into the Media Library ContentProviderContentresolver resolver =Getcontentresolver (); Uri URI=Resolver.insert (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); //send a broadcast. Notice that this media file is ready for use.Sendbroadcast (NewIntent (Intent.action_media_scanner_scan_file,uri));
View Code
Related Article

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.