3

我正在将音频文件列表存储到我的文档目录中,然后获取它们。

它给了我一个音频文件列表,同时它给了我一个带有 name 的文件@".DS_Store"。在获取内容时,我想保留此文档目录文件。

@".DS_Store"除了从数组中删除它或应用检查之外,有什么方法可以在获取音频列表时摆脱它。

这究竟是什么原因。?

#pragma mark - Saving Audio in Document Directory
-(void)saveAudioinDocumentDirectory:(ASIHTTPRequest *)theRequest
{
    /*save the Audio file in Document Directory */
    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSLog(@"GOT THE SIZe OF AUDIO %d",[[theRequest responseData] length]);
    NSLog(@"AUDIO ID IS %@",[[theRequest userInfo] valueForKey:@"audioIndex"]);

    /*Get the Path  to Application Documents Directory*/
    NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    /*append the neccessary file extension */

    NSString *filepathStr=[NSString stringWithFormat:@"/%@/%@.mp3",docDir,[NSString stringWithFormat:@"%@",[[theRequest userInfo] valueForKey:@"audioIndex"]]];

    /*Check if my crrent file exists in the Documents Directory*/

    if(![fileManager fileExistsAtPath:filepathStr])
    {
        /* file doesnt exists */

        /*create a NSdata of File*/
        NSData *data=[NSData dataWithData:[theRequest responseData]];
        NSLog(@"%@",filepathStr);

        if ([data length] >0 ){


            /*write the File at the Location in Documents Directory */

            [data writeToFile:filepathStr atomically:YES];
            NSLog(@"Successfully saved the file to %@", filepathStr);
        }
        else if([data length] == 0)
        {
            NSLog(@"Nothing was downloaded.");
        }

    }
    /*After saving fetch the path til documents Directory*/
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    /*Get the Path for Files */
    NSString *s=[folders objectAtIndex:0];

    /*Fetch the list of Files stored in Documents Directory*/

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
    NSLog(@"TOTAL NUMBER OF AUDIO FILES %d %@",[contents count],[contents objectAtIndex:0]);

    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document Intro IS %@",Audiolistforstr);

        /*Intro*/
        [AudioListArrForIntro removeAllObjects];
        [AudioListArrForIntro addObjectsFromArray:contents];
        if([AudioListArrForIntro containsObject:@".DS_Store"])
        {
            [AudioListArrForIntro removeObject:@".DS_Store"];

        }
        NSLog(@"FINAL LIST %@",AudioListArrForIntro);

    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document credit IS %@",Audiolistforstr);

        /*credits*/
        [AudioListArrForCredits removeAllObjects];
        [AudioListArrForCredits addObjectsFromArray:contents];
        if([AudioListArrForCredits containsObject:@".DS_Store"])
        {
            [AudioListArrForCredits removeObject:@".DS_Store"];

        }
    NSLog(@"FINAL LIST %@",AudioListArrForCredits);

    }


    /* Did we find anything? */
    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Intro*/
        if ([AudioListArrForIntro count] == 0)
        {

        }
        else
        {
            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioIntroTag];
            [(UITableView *)[vw viewWithTag:tblIntroAudioListTag] reloadData];
        }
    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {
        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Credits*/
        if ([AudioListArrForCredits count] == 0)
        {
        }

        else
        {

            /*AudioListForCredits*/

            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioCreditsTag];
            [(UITableView *)[vw viewWithTag:tblCreditsAudioListTag] reloadData];
        }

    }

}

任何帮助,将不胜感激。

谢谢维卡斯

4

4 回答 4

6

你可以检查.DS_StoreNSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];线............也就是说,

NSMutableArray * dirContents = [[NSMutableArray alloc] initWithArray:docDir];
if([docDir containsObject:@".DS_Store"])
{
    [dirContents removeObject:@".DS_Store"];
}

这样,dirContents删除 的条目.DS_Store

于 2012-12-26T06:27:54.790 回答
2

过滤您的文档目录内容。例如,如果您有扩展名为 .mp3 的音频文件,那么您可以获得所有 mp3 文件,如下所示:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];

directoryContent = [directoryContent filteredArrayUsingPredicate:
                                            [NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"mp3"]];

这将忽略除 mp3 文件之外的所有其他文件。万事如意!!!

于 2012-12-26T07:30:05.527 回答
1

您正在使用的 API:

NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];

返回在路径中找到的所有文件,其中包括.DS_Store”文件

我建议将“ contents”分配给可变数组,例如:

NSMutableArray * contents = 
    [[NSMutableArray alloc] initWithArray: [fileManager contentsOfDirectoryAtPath:s error:NULL]];` 

并遍历数组以查找并删除任何和所有没有“.mp3”作为路径扩展名的文件。

我还建议不要以大写字母开头任何变量(例如,代替“ Audiolistforstr”,使用“ audiolistforstr”,甚至更好,“ arrayofAudioFiles”)。Objective C 的最佳实践是以小写字母开头的所有变量和方法。

于 2012-12-26T06:28:11.760 回答
1

它运作良好..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (int i = 0; i < [imageFilenames count]; i++)
    {

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];

        if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
        {
          UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
          UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
        }
    }
于 2013-11-19T04:06:05.773 回答