Captured Video on iPhone, trying to move it to another directory - iphone

Using UIImagePickerController, I have captured a video. When I call [picker stopVideoCapture], then the following delegate method is called:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSLog(#"attempting to copy: %# to: %#", [url absoluteString], [NSString stringWithFormat:#"%#/rawMovie.MOV",path]);
if ([fileManager moveItemAtPath:[url absoluteString] toPath:[NSString stringWithFormat:#"%#/rawMovie.MOV",path] error:&error] != YES)
NSLog(#"Can't move file with error: %#", [error localizedDescription]);
}
When this method is called however, it returns an error reading:
The operation couldn’t be completed. (Cocoa error 4.)
As far as I have been able to tell, this means that the file cannot be copied for some unknown reason. Can anyone give me a better answer as to why this error is being thrown? Or, better yet, can anyone tell me the best way to save the captured video directly to the app's documents directory?
Thanks,
James

Why dont you put breakpoints & see the output of *path and url. Also do you have enough disk space to store your video on the device?

I think you need to use the default file manager:
NSFileManager* fileManager = [NSFileManager defaultManager];
You can also log the error's userinfo for (hopefully) more details.
NSLog(#"Can't move file with error: %#", [error userInfo]);

Related

Upload audio file from document directory to iCloud

I am trying to upload audio file like session1.mp3 from document directory to iCloud using the following code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"session1.mp3"];
NSURL* sourceURL = [[NSURL alloc] initFileURLWithPath:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSLog(#"File found!");
}
else
{
NSLog(#"File not found!");
}
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq)
{
NSLog(#"iCloud access at %#", ubiq);
NSError* error = nil;
NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:#"Documents"] URLByAppendingPathComponent:#"session1.mp3"];
[[NSFileManager defaultManager] setUbiquitous:YES
itemAtURL:sourceURL
destinationURL:destinationURL
error:&error];
if(error != nil)
{
NSLog(#"Error while uploading the file: %#", error);
}
else
{
NSLog(#"File ulpoaded successfully");
}
}
else
{
NSLog(#"No iCloud access");
}
The file i am trying to upload exists (the "File found" is printed), but the uploading it to iCloud generates the following error
Error while uploading the file: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1f03d9f0 {NSURL=file://localhost/var/mobile/Applications/20D82CDA-021E-4067-B9AB-C0197A6FA834/dox.app/session1.mp3, NSUnderlyingError=0x1f03d990 "The operation couldn’t be completed. Operation not permitted"}
Nothing inherently wrong with your code.
Are you checking the ubiquityIdentityToken anywhere to make sure it is available?
[ [ NSFileManager defaultManager ] ubiquityIdentityToken ];
Also, in your logging of a successful setUbiquitous, "upload successful" is not exact: the item was simply moved to the ubiquity container, and will be uploaded when the demon feels like it. You can check on the upload status through NSMetadataQuery.
You are not supposed to use iCloud to store this media.
Please review the Guidelines given by Apple.

Not been able to writeToFile iphone

I am using following code to download an slqite file and storing it
self.responseData = NSMutableData
I receive responseData = 2048bytes. working well.
However white writing it does create a file myFile.sqlite but it is of Zero bytes.
What is wrong?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
if(self.responseData)
{
dbPath = [dbPath stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
[self.responseData writeToFile:dbPath atomically:YES];
[self performSegueWithIdentifier:#"list" sender:self];
}
}
[self.alertView removeFromSuperview];
}
-(NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSURL *url = [[NSURL alloc] initWithString:[self.selectedBtn.dbPath lastPathComponent]];
return [documentsDir stringByAppendingPathComponent:[url lastPathComponent]];
}
I am getting error
`The operation couldn’t be completed. (Cocoa error 4.)`
dbPath : /Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/00027635-CE9C-48C3-8000-64CA1E6532F1/Documents/music.sqlite/music.sqlite
You are appending [self.selectedBtn.dbPath lastPathComponent] twice, once in getDBPath, and again ion connctionDidFinishLoading.
Pick one of those instances to remove.
Documents/music.sqlite/music.sqlite
Nah, this is not a valid path... You meant Documents/music.sqlite, didn't you? Also, I don't see why you're raping poor NSURL for an unrelated task.
return [documentsDir stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
Also, make sure self.selectedBtn.dbPath also is indeed a valid path and not junk (I could imagine it is).

How to move a movie from Camera Roll to app's Documents folder?

How can I copy/move a movie from Camera Roll to an app's own Documents folder?
You can implement your delegate method imagePickerController:didFinishPickingMediaWithInfo: like this –
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL * fileURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * storePath = [documentsDirectory stringByAppendingPathComponent:#"myMovie.MOV"];
NSError * error = nil;
[[NSFileManager defaultManager] copyItemAtURL:fileURL
toURL:[NSURL fileURLWithPath:storePath]
error:&error];
if ( error )
NSLog(#"%#", error);
[self dismissModalViewControllerAnimated:YES];
}
Once the user selects a movie, you get a file URL to a temporary file using the key UIImagePickerControllerMediaURL in the info dictionary passed as argument to the delegate method. You can get that and then copy the file to the documents directory using NSFileManager.

how to import video from iphone with no time duration

can any one help me please
i just want to pick video from iphone which already recorded with help of uiimagepicker controller.
i want to copy my apps document directory.
---------------------------------- prashant
I am unsure of what you mean with no time duration. But you can do the copying/moving in the UIImagePickerDelegate method.
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *movieURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *saveURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:#"movie.mov"]];
NSError *error;
if (!([[NSFileManager defaultManager] moveItemAtURL:movieURL toURL:saveURL error:&error])) {
NSLog(#"Error saving: %#", [error localizedDescription]);
}
[picker dismissModalViewControllerAnimated:YES];
}

deleting image I've saved before exiting the program

I'm creating an app that uses pictures from the camera.
I ask the user to take a picture then save it in my document folder. It appears that it is not deleted when the program exits.
How and where is this best done (appdelegate perhaps)?
If you want to get the files deleted when application exit, I would recommend you to use the /tmp directory instead. Read the documentation of File System.
Also if you just want to delete the files in documents directory, do it from applicationWillTerminate as follows:
NSArray *homePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *homeDir = [homePaths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error=nil;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:homeDir error:nil];
for (NSString *file in contents) {
BOOL success = [fileManager removeItemAtPath:[homeDir stringByAppendingPathComponent:file] error:&error];
if(!success){
NSLog(#"couldn't delete file: %#",error);
}
}
Delete your picture in
- (void)applicationWillTerminate:(UIApplication *)application
of you app delegate;