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 final 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());
intent.setComponent(
new ComponentName("com.android.providers.media",
"com.android.providers.media.MediaScannerService"));
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 (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 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 (false) {
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 (false) {
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.
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 (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 void | scanFile(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.
ClientProxy client = new ClientProxy(paths, mimeTypes, callback);
MediaScannerConnection connection = new MediaScannerConnection(context, client);
client.mConnection = connection;
connection.connect();
|
|