Dilip Bhuva
Dilip Bhuva

Reputation: 180

NSData null when load file from document directory

NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                  NSUserDomainMask,
                                                                  YES) lastObject]
                             stringByAppendingPathComponent:@"spring123.mp3"];
     NSLog(@"mp3 data %@",mp3FileName);

    NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
    NSLog(@"mp3 data %@",mp3Data);

This is giving me in log : mp3 data <>

What is the mistake done by me ?

What to do to convert this file into NSData ? I want to send this in Email.

Upvotes: 3

Views: 156

Answers (1)

Jeyamahesan
Jeyamahesan

Reputation: 1111

First you need to check the file exist at path

BOOL isFileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

if yes then only dataWithContentsOfFile: will create and returns the data object otherwise it returns nil.

Upvotes: 1

Related Questions