Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file size of file from iPhone documents folder

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.

thanks

       NSURL*videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

       NSLog(@"found a video");

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

       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
      [dateFormat setDateFormat:@"dd-MM-yyyy_HH:mm:SS"];
      NSDate *now = [[[NSDate alloc] init] autorelease];
      NSDate* theDate = [dateFormat stringFromDate:now];



      NSString*myDate=[dateFormat stringFromDate:theDate];
      NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

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



    NSString*test=@"test";


    NSString*testUser=[test stringByReplacingOccurrencesOfString:@" " withString:@""];



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

    BOOL success = [videoData writeToFile:videopath atomically:NO];
    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
            NSLog(@"video path --> %@",videopath);

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

    //  durationTime=[NSString stringWithFormat:@"%d",seconds];

    //  insertTime=[NSString stringWithFormat:@"%d",seconds];



    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;
like image 813
Aijaz Ali Avatar asked Oct 08 '13 04:10

Aijaz Ali


People also ask

How do I see the size of my Documents folder?

The quickest way to see the size of the home folder is to Right-click the My Documents folder on the Desktop and choose Properties. 2. On the General tab of the Properties window you will see the size of this folder.

How do I find file size on iPhone?

Tap on '+' and search for 'Get details of file' and select that action. Step #5. Tap on the highlighted 'Detail' and select 'File Size' from the pop-up.


2 Answers

Note: Above Method is deprecated so, use below method

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

like image 72
Hardik Thakkar Avatar answered Nov 16 '22 22:11

Hardik Thakkar


Try this its help you

 NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:videopath traverseLink:YES];
     fileSize = [fileDictionary fileSize];
like image 34
Sumit Mundra Avatar answered Nov 16 '22 23:11

Sumit Mundra