MediaScannerConnectionpublic class MediaScannerConnection extends Object implements android.content.ServiceConnectionMediaScannerConnection 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 |
Methods Summary |
---|
public void | connect()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 void | disconnect()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 boolean | isConnected()Returns whether we are connected to the media scanner service
return (mService != null && mConnected);
| public void | onServiceConnected(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 void | onServiceDisconnected(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 void | scanFile(java.lang.String path, java.lang.String mimeType)Requests the media scanner to scan a file.
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);
}
}
}
|
|