Friday, December 10, 2010

Media Scanner : Android

What is Media Scanner?
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider. The MediaScannerConnectionClient provides an interface for the media scanner service to return the Uri for a newly scanned file to the client of the MediaScannerConnection class.


In simple terms, when ever you create an image, record video or audio. Your program must update media scanner with details of the file, so that other applications like gallery, video player, media player etc. will be able to identify  the presence of the new file created.
Here is the code to update media scanner.



String name= /** NAME OF YOUR FILE **/
String path= /** YOUR FILE PATH **/
File k= new File(path, name);
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Video.Media.TITLE, name);
values.put(MediaStore.Video.Media.DATE_ADDED, (int)(current/1000));
// CHANGE THIS LINE TO SUIT THE MIME TYPE OF YOUR FILE
values.put(MediaStore.Video.Media.MIME_TYPE, "video/3gpp");
values.put(MediaStore.Video.Media.DATA,k.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,newUri ));
                               
------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment