0

我正在使用 writeToFile 将音频文件写入本地 iphone 目录。下次我启动应用程序时,路径/ URL 不再存在,我无法检索我的文件。

将不胜感激任何帮助。

保存:

    NSArray *dirPaths;
    NSString *docsDir; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *tempFileName = [[NSString alloc] init];
    tempFileName = [[alertView textFieldAtIndex:0]text];
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.wav", tempFileName]];
    BOOL success = [videoData writeToFile:soundFilePath atomically:YES];

阅读:

if ([[NSFileManager defaultManager] fileExistsAtPath:[urlArray objectAtIndex:indexPath.row]])

其中 urlArray 包含文件的路径,并且此 If 语句为 false。

4

2 回答 2

1

为什么不将 filePath 保存在 NSMutableArray 和 NSUserDefault 中

 //After writing file in doc dir
 if(success)
 {
   // Store the data
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   if(![defaults objectForKey:@"filePaths"]) //creating mutable array for first time
   {
      NSMutableArray *arrFilePaths = [NSMutableArray alloc]init];
      [defaults setObject:arrFilePaths forKey:@"filePaths"];
   }
   else //saving new soundFilePath in mutable array
   {
      NSMutableArray *arrFilePaths = [defaults objectForKey:@"filePaths"];
      [arrFilePaths addObject:soundFilePath];
      [defaults setObject:arrFilePaths forKey:@"filePaths"];
      [defaults synchronize];
   }

 }

现在你可以这样阅读:

if([[defaults objectForKey:@"filePaths"] objectAtIndex:indexPath.row])
{
  if ([[NSFileManager defaultManager] fileExistsAtPath:[[defaults objectForKey:@"filePaths"] objectAtIndex:indexPath.row]])
  {
     //here is your file
  }
}
于 2012-08-28T05:06:47.210 回答
0

每次启动应用程序时,您都需要像上面那样在 NSDocument 目录中再次搜索路径。

您可以在 AppDelegate 类 didFinishLaunching 中为 filePath 创建实例变量来记录路径。

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *tempFileName = [[NSString alloc] initWithString:@"PutYourFileName"];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.wav", tempFileName]];
于 2012-08-28T04:54:27.820 回答