如何使用枚舉NSMoviesDirectory或NSMusicDirectory。文檔說,我們可以從ios 4開始使用它們,但在我的應用程序中我找不到如何在那裏複製文件的方法? 爲什麼蘋果如果我們不能使用它們就添加了這些枚舉?NSSearchPathDirectory的一些用途枚舉
0
A
回答
-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;
}
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];
}
相關問題
- 1. 這些#define在枚舉中的用途是什麼?
- 2. 串聯枚舉和Linq中的一些
- 3. 枚舉與一些重複值?
- 4. 顯示只有一些枚舉選項
- 5. 將枚舉數據綁定到WPF中的ComboBox,篩選一些枚舉
- 6. 使用列枚舉的LINQ表枚舉
- 7. 枚舉的枚舉[JAVA]
- 8. 帶枚舉的MySQL枚舉
- 9. Python 3枚舉:枚舉繼承另一個枚舉不起作用?
- 10. 如何從所有枚舉類型中排除某些枚舉
- 11. 這些枚舉/開始枚舉隊列嗎?
- 12. 在枚舉中枚舉是否有一個通用的方法?
- 13. 使用另一個枚舉中的特定值創建枚舉
- 14. 在枚舉枚舉中調用.Distinct()
- 15. 用Spring Data REST公開枚舉枚舉
- 16. 轉換枚舉來枚舉
- 17. Java類枚舉枚舉類
- 18. 重新枚舉枚舉
- 19. Java枚舉找到枚舉
- 20. 在枚舉中枚舉
- 21. 如何使用枚舉來定義另一個枚舉?
- 22. 你如何使用枚舉與另一個枚舉?
- 23. 使用枚舉來定義另一個枚舉
- 24. 如何使用linq將枚舉值與某些枚舉值相匹配
- 25. 使用枚舉
- 26. 使用枚舉
- 27. 枚舉用delphi
- 28. 使用枚舉
- 29. 使用枚舉
- 30. 枚舉按鈕的NSMutableArray添加一些到另一個NSMutableArray
這項工作的罰款對我來說Mr.toohtik – Vinodh 2013-03-20 13:00:28
都能跟得上。謝謝你的迴應。但它首先是我試圖去做的。對我來說,它不工作,因爲錯誤:「錯誤域= NSCocoaErrorDomain代碼= 513 NSFileWriteNoPermissionError」。但爲什麼蘋果需要我們分離資源?原因之一 - 它是因爲蘋果在應用程序中掃描每個電影文件夾,然後爲視頻應用程序生成所有電影列表。但我無法檢查這個問題,因爲無法創建文件夾並在那裏複製 – toohtik 2013-03-20 13:19:15