public class MediaScannerConnection extends Object implements ServiceConnection
Modifier and Type | Class and Description |
---|---|
static interface |
MediaScannerConnection.MediaScannerConnectionClient
An interface for notifying clients of MediaScannerConnection
when a connection to the MediaScanner service has been established
and when the scanning of a file has completed.
|
static interface |
MediaScannerConnection.OnScanCompletedListener
Interface for notifying clients of the result of scanning a
requested media file.
|
Constructor and Description |
---|
MediaScannerConnection(Context context,
MediaScannerConnection.MediaScannerConnectionClient client)
Constructs a new MediaScannerConnection object.
|
Modifier and Type | Method and Description |
---|---|
void |
connect()
Initiates a connection to the media scanner service.
|
void |
disconnect()
Releases the connection to the media scanner service.
|
boolean |
isConnected()
Returns whether we are connected to the media scanner service
|
void |
onServiceConnected(ComponentName className,
IBinder service)
Part of the ServiceConnection interface.
|
void |
onServiceDisconnected(ComponentName className)
Part of the ServiceConnection interface.
|
static void |
scanFile(Context context,
String[] paths,
String[] mimeTypes,
MediaScannerConnection.OnScanCompletedListener callback)
Convenience for constructing a
MediaScannerConnection , calling
connect() on it, and calling scanFile(java.lang.String, java.lang.String) with the given
path and mimeType when the connection is
established. |
void |
scanFile(String path,
String mimeType)
Requests the media scanner to scan a file.
|
public MediaScannerConnection(Context context, MediaScannerConnection.MediaScannerConnectionClient client)
context
- the Context object, required for establishing a connection to
the media scanner service.client
- an optional object implementing the MediaScannerConnectionClient
interface, for receiving notifications from the media scanner.public void connect()
MediaScannerConnection.MediaScannerConnectionClient.onMediaScannerConnected()
will be called when the connection is established.public void disconnect()
public boolean isConnected()
public void scanFile(String path, String mimeType)
MediaScannerConnection.MediaScannerConnectionClient.onScanCompleted(String, Uri)
is called.path
- the path to the file to be scanned.mimeType
- an optional mimeType for the file.
If mimeType is null, then the mimeType will be inferred from the file extension.public static void scanFile(Context context, String[] paths, String[] mimeTypes, MediaScannerConnection.OnScanCompletedListener callback)
MediaScannerConnection
, calling
connect()
on it, and calling scanFile(java.lang.String, java.lang.String)
with the given
path and mimeType when the connection is
established.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
MediaScannerConnection.MediaScannerConnectionClient.onScanCompleted(String, Uri)
is called.paths
- Array of paths to be scanned.mimeTypes
- Optional array of MIME types for each path.
If mimeType is null, then the mimeType will be inferred from the file extension.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.scanFile(String, String)
public void onServiceConnected(ComponentName className, IBinder service)
onServiceConnected
in interface ServiceConnection
className
- The concrete component name of the service that has
been connected.service
- The IBinder of the Service's communication channel,
which you can now make calls on.public void onServiceDisconnected(ComponentName className)
onServiceDisconnected
in interface ServiceConnection
className
- The concrete component name of the service whose
connection has been lost.