FileDocCategorySizeDatePackage
MediaScannerConnection.javaAPI DocAndroid 5.1 API9944Thu Mar 12 22:22:30 GMT 2015android.media

MediaScannerConnection

public class MediaScannerConnection extends Object implements android.content.ServiceConnection
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.

Fields Summary
private static final String
TAG
private android.content.Context
mContext
private MediaScannerConnectionClient
mClient
private android.media.IMediaScannerService
mService
private boolean
mConnected
private final IMediaScannerListener.Stub
mListener
Constructors Summary
public MediaScannerConnection(android.content.Context context, MediaScannerConnectionClient client)
Constructs a new MediaScannerConnection object.

param
context the Context object, required for establishing a connection to the media scanner service.
param
client an optional object implementing the MediaScannerConnectionClient interface, for receiving notifications from the media scanner.


                      
       
                                                                    
             
    

                                   
         
                                
          

                                                                    
             
    

                                             
         
        mContext = context;
        mClient = client;
    
Methods Summary
public voidconnect()
Initiates a connection to the media scanner service. {@link MediaScannerConnectionClient#onMediaScannerConnected()} will be called when the connection is established.

        synchronized (this) {
            if (!mConnected) {
                Intent intent = new Intent(IMediaScannerService.class.getName());
                intent.setComponent(
                        new ComponentName("com.android.providers.media",
                                "com.android.providers.media.MediaScannerService"));
                mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
                mConnected = true;
            }
        }
    
public voiddisconnect()
Releases the connection to the media scanner service.

        synchronized (this) {
            if (mConnected) {
                if (false) {
                    Log.v(TAG, "Disconnecting from Media Scanner");
                }
                try {
                    mContext.unbindService(this);
                } catch (IllegalArgumentException ex) {
                    if (false) {
                        Log.v(TAG, "disconnect failed: " + ex);
                    }
                }
                mConnected = false;
            }
        }
    
public synchronized booleanisConnected()
Returns whether we are connected to the media scanner service

return
true if we are connected, false otherwise

        return (mService != null && mConnected);
    
public voidonServiceConnected(android.content.ComponentName className, android.os.IBinder service)
Part of the ServiceConnection interface. Do not call.

        if (false) {
            Log.v(TAG, "Connected to Media Scanner");
        }
        synchronized (this) {
            mService = IMediaScannerService.Stub.asInterface(service);
            if (mService != null && mClient != null) {
                mClient.onMediaScannerConnected();
            }
        }
    
public voidonServiceDisconnected(android.content.ComponentName className)
Part of the ServiceConnection interface. Do not call.

        if (false) {
            Log.v(TAG, "Disconnected from Media Scanner");
        }
        synchronized (this) {
            mService = null;
        }
    
public voidscanFile(java.lang.String path, java.lang.String mimeType)
Requests the media scanner to scan a file. Success or failure of the scanning operation cannot be determined until {@link MediaScannerConnectionClient#onScanCompleted(String, Uri)} is called.

param
path the path to the file to be scanned.
param
mimeType an optional mimeType for the file. If mimeType is null, then the mimeType will be inferred from the file extension.

        synchronized (this) {
            if (mService == null || !mConnected) {
                throw new IllegalStateException("not connected to MediaScannerService");
            }
            try {
                if (false) {
                    Log.v(TAG, "Scanning file " + path);
                }
                mService.requestScanFile(path, mimeType, mListener);
            } catch (RemoteException e) {
                if (false) {
                    Log.d(TAG, "Failed to scan file " + path);
                }
            }
        }
    
public static voidscanFile(android.content.Context context, java.lang.String[] paths, java.lang.String[] mimeTypes, android.media.MediaScannerConnection$OnScanCompletedListener callback)
Convenience for constructing a {@link MediaScannerConnection}, calling {@link #connect} on it, and calling {@link #scanFile} with the given path and mimeType when the connection is established.

param
context The caller's Context, required for establishing a connection to the media scanner service. Success or failure of the scanning operation cannot be determined until {@link MediaScannerConnectionClient#onScanCompleted(String, Uri)} is called.
param
paths Array of paths to be scanned.
param
mimeTypes Optional array of MIME types for each path. If mimeType is null, then the mimeType will be inferred from the file extension.
param
callback Optional callback through which you can receive the scanned URI and MIME type; If null, the file will be scanned but you will not get a result back.
see
#scanFile(String, String)

        ClientProxy client = new ClientProxy(paths, mimeTypes, callback);
        MediaScannerConnection connection = new MediaScannerConnection(context, client);
        client.mConnection = connection;
        connection.connect();