klient
klient

Reputation: 31

dataWithContentsOfFile return null

NSString *path = dict[@"fileURL"];
NSData *imageData = [NSData dataWithContentsOfFile:path];

imageData is null

dict[@"fileURL"] =
/var/mobile/Containers/Data/Application/0906AB53-CE1F-45EF-8E00-9D0B7C98956C/Documents/ccc/image/1446625127.png

I download the device container and the image did exist. What's wrong?

Upvotes: 2

Views: 2086

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

It's failed to retrieve your file. Use -dataWithContentsOfFile:options:error: to find why it is failed.

NSError* error = nil;
NSString *path = dict[@"fileURL"];
NSData *data = [NSData dataWithContentsOfFile:path options: 0 error: &error];

if (data == nil)
{
   NSLog(@"Failed to read file, error %@", error);
}
else
{
    // parse the value
}

Upvotes: 1

V.J.
V.J.

Reputation: 9590

I thing you should need to use [NSFileManager defaultManager] to store and fetch the image in your application storage instead of writing static path.

Static path is ok withing the simulator but in the real environment you should need to have the actual path. So [NSFileManager defaultManager] will help you.

if([[NSFileManager defaultManager] fileExistsAtPath:filepath)
{
   NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];
}
else
{
   NSLog(@"File not exits");
}

In filepath you need to pass only your folder and file name. not full path like yours.

IE.

filepath = "Documents/ccc/image/1446625127.png";

NOTE: This is not a actual answer. This will help you to manage the image into your application storage.

Upvotes: 0

Related Questions