How to Run Media Scanner in Android

Media scanning specific folder

Use below code to get the files from specific folder:

File folder_file = new File("give specific folder path");
File[] files = folder_file.listFiles();
if (files != null) {
for (File file : files) {
// checking the File is file or directory
if (file.isFile()) {
String path = file.getAbsolutePath();
String extension = path
.substring(path.lastIndexOf(".") + 1);
// if the file is audio type, then save it to the database
if (extension.equalsIgnoreCase("mp4")) {
System.out.println(path + " is a media file ");
}
}
}
}

Try this for scanning files:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));

Android How to use MediaScannerConnection scanFile

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));

How to use MediaScannerConnection scanFile?

    MediaScannerConnection.scanFile(this, new String[]{file.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
}
});

Trigger mediascanner on specific path (folder), how to?

Hey I found out how to do it with a very simple code.

Just call this line of code:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

This should trigger mediascanner.



Related Topics



Leave a reply



Submit