By default, media files created by your application will be unavailable to other
applications. As a result, it’s good practice to insert it into the Media Store
to make it available to other applications.
Android provides two alternatives for inserting media into the Media Store,
either using the Media Scanner to interpret your file and insert it automatically,
or manually inserting a new record in the appropriate Content Provider.
Using the Media Scanner
If you have recorded new media of any kind, the MediaScannerConnection
class provides a simple way for you to add it to the Media Store without
needing to construct the full record for the Media Store Content Provider.
Before you can use the scanFile method to initiate a content scan on your file,
you must call connect and wait for the connection to the Media Scanner to complete.
This call is asynchronous, so you will need to implement
a MediaScannerConnectionClient to notify you when the connection has been
made. You can use this same class to notify you when the scan is complete,
at which point you can disconnect your Media Scanner Connection.
The skeleton code for creating a new MediaScannerConnectionClient
MediaScannerConnectionClient mediaScannerClient =
new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null;
{
msc = new MediaScannerConnection(getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile("/sdcard/test1.jpg", null);
}
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
};
댓글 없음:
댓글 쓰기