我如何使用枚举 NSMoviesDirectory 或 NSMusicDirectory。文档说我们可以从 ios 4 开始使用他们的,但在我的应用程序中我找不到任何方法如何在那里复制文件?如果我们不能使用它们,为什么苹果会添加这些枚举?
问问题
785 次
2 回答
1
默认情况下,“电影”、“音乐”和“图片”不会在目录中。您必须创建路径并放置数据文件。
例如:NSMoviesDirectory
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSMoviesDirectory,NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSError * error;
if (![[NSFileManager defaultManager] fileExistsAtPath: documentsDirectory])
{
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error];
NSString * resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sleep.mp4"];
[[NSFileManager defaultManager] copyItemAtPath:resourceDBFolderPath toPath:[documentsDirectory stringByAppendingPathComponent:@"sleep.mp4"] error:&error];
}
else
{
NSString * resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sleep.mp4"];
[[NSFileManager defaultManager] copyItemAtPath:resourceDBFolderPath toPath:[documentsDirectory stringByAppendingPathComponent:@"sleep.mp4"] error:&error];
}
于 2013-03-20T12:43:03.710 回答
-1
只需编写一个示例,默认情况下不存在音乐目录(或其他内容),您应该以编码方式创建它。样本:
- (void)testDir
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES);
NSString *musicDirectory = [paths objectAtIndex:0];
NSFileManager *defaultFileManager = [NSFileManager defaultManager];
if (![defaultFileManager fileExistsAtPath:musicDirectory]) {
[defaultFileManager createDirectoryAtPath:musicDirectory withIntermediateDirectories:NO attributes:nil error:NULL];
}
NSString *testDataFilePath = [musicDirectory stringByAppendingPathComponent:@"test.mp3"];
unsigned int bufferLength = 1024;
void *buffer = malloc(1024 * sizeof(char));
NSData *testData = [[NSData alloc] initWithBytes:buffer length:bufferLength];
free(buffer); buffer = NULL;
if (![testData writeToFile:testDataFilePath atomically:NO]) {
NSLog(@"fail to save data");
}
[testData release]; testData = nil;
}
于 2013-03-20T12:34:00.103 回答