Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run media scanner in android

I want run the media scanner while capturing the image. After capturing, the image it is updated in grid view. For that I need to run media scanner. I found two solutions to run media scanner one is the broadcast event and other one is running media scanner class. I think in Ice Cream Sandwich (4.0) media scanner class is introduced.Before versions need to set broadcast event for running media scanner.

can any one guide me how to run media scanner in right way.

like image 536
prudhvireddy Avatar asked Nov 07 '12 13:11

prudhvireddy


People also ask

How to scan media files on Android?

The app is called “ media.Re.Scan: media scanner for android2.3 to 7.1 “. This application will let you choose the type of content you want to be scanned y=by the android OS. Suppose if you have added an image which doesn’t show up, you don’t have to scan each and every files in your memory.

How does mediascanner work on Android?

When Android boots up, the mediascanner service is launched and runs through the entire external storage to find if there is any new media content if it finds one then, It adds an entry of that media content into the content database

Is it better to run media scanner on a specific file?

I have found it best (faster/least overhead) to run media scanner on a specific file (vs running it to scan all files for media), if you know the filename. Here's the method I use:

Can I Run Media Scanner on root folder?

Running the Media Scanner is an expensive operation and the situation gets even worse when the user has got a lot of files in the storage and deep folder structures. Before Android 4.4 Keep it straight and simple. If you are targeting your app before Android 4.4. But keep in mind not to use it on the root directory unless absolutely necessary.


1 Answers

I have found it best (faster/least overhead) to run media scanner on a specific file (vs running it to scan all files for media), if you know the filename. Here's the method I use:

/**
 * Sends a broadcast to have the media scanner scan a file
 * 
 * @param path
 *            the file to scan
 */
private void scanMedia(String path) {
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    Intent scanFileIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
    sendBroadcast(scanFileIntent);
}

When needing to run on multiple files (such as when initializing an app with multiple images), I keep a collection of the new images filenames while initializing, and then run the above method for each new image file. In the below code, addToScanList adds the files to scan to an ArrayList<T>, and scanMediaFiles is used to initiate a scan for each file in the array.

private ArrayList<String> mFilesToScan;

/**
 * Adds to the list of paths to scan when a media scan is started.
 * 
 * @see {@link #scanMediaFiles()}
 * @param path
 */
private void addToScanList(String path) {
    if (mFilesToScan == null)
        mFilesToScan = new ArrayList<String>();
    mFilesToScan.add(path);
}

/**
 * Initiates a media scan of each of the files added to the scan list.
 * 
 * @see {@see #addToScanList(String)}
 */
private void scanMediaFiles() {
    if ((mFilesToScan != null) && (!mFilesToScan.isEmpty())) {
        for (String path : mFilesToScan) {
            scanMedia(path);
        }
        mFilesToScan.clear();
    } else {
        Log.e(TAG, "Media scan requested when nothing to scan");
    }
}
like image 193
bobnoble Avatar answered Oct 07 '22 06:10

bobnoble