Reputation: 90736
I'm using rn-fetch-blob 0.10.8 with RN 0.49 on iOS with iPhone 6 Simulator.
I'm using the react-native-image-resizer module to resize an image and then I use react-native-fetch-blob to move that image to the right location. However, when doing so I'm getting the error:
The file “6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg” couldn’t be opened because there is no such file.
Before doing the copy, I'm printing the source and destination to the console:
"Moving file:///Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg => /Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Documents/testing.jpg"
I can verify that the path //Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg
exist as I can open it in Finder. However for some reason rn-fetch-blob doesn't find it.
Any idea what could be the issue?
For information this the code I'm using:
const resizedImage = await ImageResizer.createResizedImage(localFilePath, dimensions.width, dimensions.height, format, 85);
const resizedImagePath = resizedImage.uri;
console.info('Moving ' + resizedImagePath + ' => ' + targetPath);
await RNFetchBlob.fs.cp(resizedImagePath, targetPath); // Throws error
Upvotes: 1
Views: 3317
Reputation: 12951
The issue is that this path isn't reachable from JavaScript, you need first save it to a valid place, like application temp
folder, and then pass the temp
folder URL.
This is how you save to temp
folder on iOS:
+(NSURL*)saveToTmpFolder:(NSData*)data {
NSString *temporaryFileName = [NSProcessInfo processInfo].globallyUniqueString;
NSString *temporaryFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[temporaryFileName stringByAppendingPathExtension:@"jpg"]];
NSURL *temporaryFileURL = [NSURL fileURLWithPath:temporaryFilePath];
NSError *error = nil;
[data writeToURL:temporaryFileURL options:NSDataWritingAtomic error:&error];
if ( error ) {
//NSLog( @"Error occured while writing image data to a temporary file: %@", error );
}
else {
//NSLog(@"Image Saved - YOU ROCK!");
}
return temporaryFileURL;
}
Or if you wan JavaScript API, react-native-fs is a great solution.
Upvotes: 3