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

I got this class:

import android.content.Context; import android.media.MediaScannerConnection; import android.net.Uri; import android.util.Log; public class MediaScannerWrapper implements MediaScannerConnection.MediaScannerConnectionClient { private MediaScannerConnection mConnection; private String mPath; private String mMimeType; // filePath - where to scan; // mime type of media to scan ie "image/jpeg". // use "*/*" for any media public MediaScannerWrapper(Context ctx, String filePath, String mime){ mPath = "/sdcard/DCIM/Camera"; mMimeType = "jpg"; mConnection = new MediaScannerConnection(ctx, this); } // do the scanning public void scan() { mConnection.connect(); } // start the scan when scanner is ready public void onMediaScannerConnected() { mConnection.scanFile(mPath, mMimeType); Log.w("MediaScannerWrapper", "media file scanned: " + mPath); } public void onScanCompleted(String path, Uri uri) { // when scan is completes, update media file tags } } 

How to use it in another class? I do not know how to use classes correctly, I tried, but nothing works. I am doing something wrong, but I do not know that, someone can help me with this.

+12
android android-mediascanner
Feb 23 2018-12-12T00:
source share
6 answers

Hey, I found out how to do this with very simple code.

Just call this line of code:

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

This should trigger the media scanner.

+9
Feb 27 '12 at 9:00
source share

Story

Before Android 4.4, we could simply send a broadcast to launch a media scanner in any specific file, folder, or even in the root directory of the repository. But with 4.4 KitKat, this has been fixed by Android developers.

Why am I saying a fix? The reason is simple. Sending broadcasts using MEDIA_MOUNTED in the root directory is very expensive. Starting a Media Scanner is an expensive operation, and the situation gets even worse when a user has many files in storage structures and deep folders.

Before Android 4.4

Keep it straight and simple. If you have targeted your app to Android 4.4. But keep in mind not to use it in the root directory unless absolutely necessary.

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

From Android 4.4

There are two ways for you.

i) The first one is very similar to the previous example, but may not work efficiently and is not recommended either.

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

ii) Now let's move on to the most recommended and effective solution to this problem.

Add paths to files of files that have been updated, for example, in String type ArrayList

 ArrayList<String> toBeScanned = new ArrayList<String>(); toBeScanned.add(item.getFilePath()); 

Now you need to run the staticFlashFile () static method of the MediaScannerConnection class and pass in a String array containing a list of all the files that have been updated and should be scanned.

You can also send a response to the listener when the scan is complete for individual files.

 String[] toBeScannedStr = new String[toBeScanned.size()]; toBeScannedStr = toBeScanned.toArray(toBeScannedStr); MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { System.out.println("SCAN COMPLETED: " + path); } }); 
+43
Aug 01 '14 at 18:33
source share

Android has a content database that is used by the media player to track all the media content present on the device.

When Android loads, the mediascanner service starts and goes through all the external memory to find if there is any new media content, if it finds it,

  • It adds a record of this media content to the content database.
  • Each entry in the content database contains media content metadata such as name, date, file size, file type, etc.
  • Therefore, when you make changes to multimedia content, you also need to update the content database.
  • If the content database is not updated, other applications will also not be able to access this specific media content.
  • Running a media scanner simply updates the content database

Instead of launching a media scanner, you can update the content database yourself and solve this problem.

Below is a description of how to insert, delete, update using a content recognizer. (Find the "Insert, Update, and Delete Data" section)

Edit: There is sample code in this answer . Check Janusz’s answer.

+5
Feb 24 '12 at 11:35
source share
  File file = new File(absolutePath); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); sendBroadcast(intent); 
+5
Mar 19 '14 at 6:34
source share
 private void galleryAddPic() { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } 

Link: http://developer.android.com/training/camera/photobasics.html#TaskGallery

Section Add the Photo to a Gallery

+1
Mar 27 '16 at 14:13
source share

As @Aritra Roy says, I decided to do an experiment on this issue. I got here:

  • Intent.ACTION_MEDIA_MOUNTED and Intent.ACTION_MEDIA_SCANNER_SCAN_FILE can take an individual file path, so sendBroadcast (new Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse (filePath))); or sendBroadcast (new Intent (Intent.ACTION_MEDIA_MOUNTED, Uri.parse (Filepath))); will be valid.
  • If you use an individual file path from Intent.ACTION_MEDIA_MOUNTED to Kitkat or higher, your application will still crash
  • If you use Intent.ACTION_MEDIA_SCANNER_SCAN_FILE or MediaScannerConnection on a device lower than Kitkat, your application will not be forced to close, but the method simply will not work the way you want.

From this experiment, I believe that the best way to handle it is

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { //something that you want to do } }); } else { context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + imagePath))); } 

Let me know if I missed something.

0
Jun 15 '16 at 6:33
source share



All Articles