简体   繁体   中英

Unable to copy file from bundle to Documents directory, why does fileExistsAtPath return error?

I'm using the following snippet of code to attempt to copy a file from my application resources directory into the Documents area. I have used the below from the PocketOCR project on Github :

// 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];
        }
    }

This is the output I get from invoking the above :

2012-06-06 14:53:42.607 MyApp[1072:707] Datapath is /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata Error opening data file /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata/eng.traineddata

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

It appears that the Error is being reported when attempting to check if fileExistsAtPath , I wouldn't expect this to throw an error, but to either return YES or NO.

Is there an easier way to copy the eng.traineddata file across, or have I made a mistake which can be corrected here?

我有像我这样的xcode项目中的eng.traineddata文件

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;
}

I hope also help this code to copy all type of file bundle to Document directory folder ..

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);

}

Thanks ..

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

stringByAppendingPathComponent:@"tessdata"

The line above required that tessdata is a physical folder (a blue color folder), not a group (yellow folder as you are showing) in Xcode project. Create a folder in your Xcode project folder using the Finder and then in Xcode drag and drop the folder to your project and select the folder reference when asked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM