How to get file size of file from iPhone documents folder
Asked Answered
H

4

8

I have in which I save video file in documents folder it works fine, but I want to get the file size of the saved file, I have searched but did not get result using NSFileManager. Here is the code which I use for saving video. I want to get the file size and show it on UILabel.

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

NSString *test = @"test";

videopath = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mp4", documentsDirectory, test]] autorelease];

BOOL success = [videoData writeToFile:videopath atomically:NO];
NSLog(@"Success: %@", success ? @"YES" : @"NO");

NSURL *movieURL = [NSURL fileURLWithPath:videopath];
AVURLAsset *avUrl = [AVURLAsset assetWithURL:movieURL];
CMTime time1 = [avUrl duration];
int seconds = ceil(time1.value / time1.timescale);

NSString *messageA = [NSString stringWithFormat:@"You have recorded video of duration of %d seconds  ", seconds];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:messageA
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

AVAsset *asset = [AVAsset assetWithURL:movieURL];// url= give your url video here
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(1, 5);// it will create the thumbnail after the 5 sec of video
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
thumbnailImageView.image = thumbnail;
Hackworth answered 8/10, 2013 at 4:59 Comment(1)
Possible duplicate of https://mcmap.net/q/146443/-finding-file-39-s-sizeFarrahfarrand
U
12

Try this to get the file size:

NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:videopath traverseLink:YES];
     fileSize = [fileDictionary fileSize];

Doc: https://developer.apple.com/documentation/foundation/nsfilemanager/1557004-fileattributesatpath

Note: this method is deprecated

Unhook answered 8/10, 2013 at 5:3 Comment(5)
This file size will be KB or BytesHackworth
this file size in bytesUnhook
fileAttributesAtPath:traverseLink: is deprecated from iOS 6Noranorah
you also use use attributesOfItemAtPath:error: method its also give the same result.Unhook
Method is deprecated. Go with - attributesOfItemAtPath:filePath error:nilTaxis
S
14

Note: Previous answer's method (fileAttributesAtPath:traverseLink:) is deprecated, so use below method (attributesOfItemAtPath:error:)

Objective-C:

NSError* error;
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:mediaURL error: &error];
NSNumber *size = [fileDictionary objectForKey:NSFileSize]; 

Swift:

do
{
    let fileDictionary = try FileManager.default.attributesOfItem(atPath: urlString)
    let fileSize = fileDictionary[FileAttributeKey.size]
    print ("\(fileSize)")
}
catch{}

** this file size in bytes

Slumber answered 20/11, 2014 at 12:9 Comment(0)
U
12

Try this to get the file size:

NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:videopath traverseLink:YES];
     fileSize = [fileDictionary fileSize];

Doc: https://developer.apple.com/documentation/foundation/nsfilemanager/1557004-fileattributesatpath

Note: this method is deprecated

Unhook answered 8/10, 2013 at 5:3 Comment(5)
This file size will be KB or BytesHackworth
this file size in bytesUnhook
fileAttributesAtPath:traverseLink: is deprecated from iOS 6Noranorah
you also use use attributesOfItemAtPath:error: method its also give the same result.Unhook
Method is deprecated. Go with - attributesOfItemAtPath:filePath error:nilTaxis
C
1

Use NSFileManager attributesOfItemAtPath:error

https://developer.apple.com/documentation/foundation/nsfilemanager/1410452-attributesofitematpath

It returns an NSDictionary of file attributes and one key it contains is NSFileSize, the size of the file.

Chagres answered 8/10, 2013 at 5:3 Comment(0)
V
0

try this way.

    // Get file size
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsDirectory stringByAppendingString:[directoryContent objectAtIndex:indexPath.row]];
NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:filePath traverseLink:YES];
if(fileAttributes != nil)
{
NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
[[cell detailTextLabel] setText:[NSString stringWithFormat:@"%@ kb", fileSize]];
NSLog(@"File size: %@ kb", fileSize);
}
Vandavandal answered 8/10, 2013 at 5:2 Comment(1)
Is this helpful to you?Vandavandal

© 2022 - 2024 — McMap. All rights reserved.