How to scan the new file in android ?

When we are going to download the file into sdcard ,it takes some time to show the availability in device.In my experience,My application downlod files to sdcard/music , but when i run the standard music player, i can’t found new mp3. When I restart my phone, music player see my new mp3.Finally i got the solution by using the Mediascaner connection.
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.The code should be like this only .
private MediaScannerConnection conn;


//Downloded file path
 private String newFilePath = "New File Path";
 conn = new MediaScannerConnection(this,
 new MediaScannerConnection.MediaScannerConnectionClient()
 {
 @Override
 public void onScanCompleted(String path, Uri uri) {

if (path.equals(newFilePath))
 {
 Log.i("Scan Status", "Completed");
 conn.disconnect();
 }
 }
 @Override
 public void onMediaScannerConnected()
 {
 // TODO Auto-generated method stub
 conn.scanFile("path of the new file", null/*MimeType*/);

 }
 });
 conn.connect();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s