无法将文件从bundle复制到Documents目录,为什么fileExistsAtPath会返回错误?

时间:2012-06-06 14:05:57

标签: objective-c ios ios5 nsfilemanager

我正在使用以下代码片段尝试将文件从我的应用程序资源目录复制到Documents区域。我在Github的PocketOCR项目中使用了以下内容:

// Set up the tessdata path. This is included in the application bundle
    // but is copied to the Documents directory on the first run.
    NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSLog(@"Datapath is %@", dataPath);
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:dataPath ]) {
        NSLog(@"File didn't exist at datapath...");
        // get the path to the app bundle (with the tessdata dir)
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"];
        if (tessdataPath) {
            [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
        }
    }

这是我从上面调用得到的输出:

  

2012-06-06 14:53:42.607 MyApp [1072:707] Datapath是   在/ var /移动/应用/ 9676D920-D6D1-4F86-9177-07CC3247A124 /文档/ tessdata   打开数据文件时出错   /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata/eng.traineddata

我的eng.traineddata文件位于我的xcode项目中,如此

在尝试检查fileExistsAtPath时是否会报告错误,我不希望这会引发错误,但要么返回YES或NO。

是否有更简单的方法来复制eng.traineddata文件,或者我犯了一个错误,可以在这里纠正?

I have the eng.traineddata file located in my xcode project like so

4 个答案:

答案 0 :(得分:4)

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSLog(@"Datapath is %@", dataPath);

// If the expected store doesn't exist, copy the default store.    
    if ([fileManager fileExistsAtPath:dataPath] == NO)
    {
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"];
        [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error];

    }


-(NSString*) applicationDocumentsDirectory{
    // Get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    return docsDir;
}

答案 1 :(得分:1)

我希望也帮助这段代码将所有类型的文件包复制到Document目录文件夹..

NSString *pathsToReources = [[NSBundle mainBundle] resourcePath];
NSString *yourOriginalDatabasePath = [pathsToReources stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name u want copy 

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];


    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name & your original path (Document path)

    if (![[NSFileManager defaultManager] isReadableFileAtPath: dbPath]) {

if ([[NSFileManager defaultManager] copyItemAtPath: yourOriginalDatabasePath toPath: dbPath error: NULL] != YES)

NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, dbPath);

}

谢谢..

答案 2 :(得分:0)

使用fileExistsAtPath:isDirectory:的方法NSFileManager并指定路径是目录,因为它是。

答案 3 :(得分:0)

stringByAppendingPathComponent:@"tessdata"

上面一行要求tessdata是一个物理文件夹(蓝色文件夹),而不是Xcode项目中的一个组(正在显示的黄色文件夹)。使用Finder在Xcode项目文件夹中创建一个文件夹,然后在Xcode中将文件夹拖放到项目中,并在询问时选择文件夹引用。