Monitoring memory card insertion/removal events
The article is believed to be still valid for the original topic scope.
Article Metadata
Compatibility
Article
Overview
This article discusses how an application can monitor the insertion and removal of memory card.
Applications can be notified about the insertion or removal of the memory card using the RFs::NotifyChange(ENotifyDisk, ...) method.
Solution
Convert application to system application
In order to receive memory card insertion and removal events, the UI application should be made a system application to prevent it from being automatically closed when removing a memory card.
Add following code to the ConstructL of the AppUI class.
// don't close me when the memory card is removed
CEikonEnv::Static()->SetSystem( ETrue );
iDiskDetector = CDiskDetector::NewL(*this, iCoeEnv->FsSession());
iDiskDetector->Start();
Add notifier to the application
In order to receive the events a notifier needs to be added to the application. This can be done by calling following code fragment
iFs.NotifyChange(ENotifyDisk, iStatus);
NotifyChange() is an asynchronous method that completes when the availability of the memory card is changed. The above code snippet is meant to be used inside a CActive-derived class. Such as...
void CDiskDetector::Start()
{
Cancel();
iFs.NotifyChange(ENotifyDisk, iStatus);
SetActive();
}
The RunL() method of the active object can be as follows,
void CDiskDetector::RunL()
{
TInt err = iStatus.Int();
if(err!=KErrCancel)
{
iObserver.NotifyDiskChange();
}
}
Check for memory card inside notifier
When the memory card is inserted / removed, the notifier / observer generates an event NotifyDiskChange. Inside this event handler the observer can then check the availability of the memory card using RFs::Volume,
void CHelloWorldAppUi::NotifyDiskChange()
{
iDiskDetector->Start();
TVolumeInfo vol;
TInt err = iCoeEnv->FsSession().Volume(vol, EDriveE);
if(err==KErrNone)
{
// the memory card is available
}
else // generally the err should be KErrNotReady
{
// the memory card is NOT available
}
}
Source Code
You can download the full example program here (the code has been verified on a S60 5th Edition device):
Related article:


(no comments yet)