×
Namespaces

Variants
Actions

Recognizing file types

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: symbianyucca (20 Mar 2007)
Last edited: hamishwillee (25 Jul 2012)

The following function shows how to determine file types.

void GetFileType(const TDesC& aFileName, TDes& aFileType)
{
TEntry FileEntry;
 
if (CCoeEnv::Static()->FsSession().Entry(aFileName,FileEntry) == KErrNone)
{
TBuf8<255> FileBuffer;
 
if(!FileEntry.IsDir())
{
TInt FileSize = FileEntry.iSize;
 
if(FileSize > 255)
{
FileSize = 255;
}
 
if(CCoeEnv::Static()->FsSession().ReadFileSection
(aFileName,0,FileBuffer,FileSize) == KErrNone)
{
RApaLsSession RSession;
if(RSession.Connect() == KErrNone)
{
TDataRecognitionResult FileDataType;
 
RSession.RecognizeData(aFileName,FileBuffer,*&FileDataType);
if(FileDataType.iConfidence > aResult.iConfidence)
{
aFileType.Copy(FileDataType.iDataType.Des());
}
 
RSession.Close();
}
}
}
}
}

If you don't have access to the file itself (for example because of the restrictions posed by the platform security) you can use this function:

void GetFileType(RFile& aFile, TDes& aFileType)
{
TInt FileSize(0);
TFileName RFileName;
 
if(aFile.Size(FileSize)== KErrNone && FullName(RFileName) == KErrNone)
{
TBuf8<255> FileBuffer;
 
if(FileSize > 255)
{
FileSize = 255;
}
 
if(aFile.Read(FileBuffer,FileSize) == KErrNone)
{
RApaLsSession RSession;
if(RSession.Connect() == KErrNone)
{
TDataRecognitionResult FileDataType;
 
RSession.RecognizeData(RFileName,FileBuffer,*&FileDataType);
if(FileDataType.iConfidence > aResult.iConfidence)
{
aFileType.Copy(FileDataType.iDataType.Des());
}
 
RSession.Close();
}
}
}
}

In both cases the type of the file is returned in the aFileName argument variable as standard MIME type (for example image/gif for gif images etc) and in case the file is not recognized the aFileName will not be modified.

This page was last modified on 25 July 2012, at 05:11.
23 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×