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 に答える