FileDocCategorySizeDatePackage
MediaScannerConnection.javaAPI DocAndroid 1.5 API6785Wed May 06 22:42:00 BST 2009android.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 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());
                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 (Config.LOGV) {
                    Log.v(TAG, "Disconnecting from Media Scanner");
                }
                try {
                    mContext.unbindService(this);
                } catch (IllegalArgumentException ex) {
                    if (Config.LOGV) {
                        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 (Config.LOGV) {
            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 (Config.LOGV) {
            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.

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. Success or failure of the scanning operation cannot be determined until {@link MediaScannerConnectionClient#onScanCompleted(String, Uri)} is called.

        synchronized (this) {
            if (mService == null || !mConnected) {
                throw new IllegalStateException("not connected to MediaScannerService");
            }
            try {
                if (Config.LOGV) {
                    Log.v(TAG, "Scanning file " + path);
                }
                mService.requestScanFile(path, mimeType, mListener);
            } catch (RemoteException e) {
                if (Config.LOGD) {
                    Log.d(TAG, "Failed to scan file " + path);
                }
            }
        }