react-native-cameraroll: I can successfully save the photo to album but an error would pop up : Unknown error from a native module

Hi guyz πŸ‘‹ I can successfully save the photo to album but an error would pop up ( see the bottom of the post ),

my version installed in package.json : β€œ@react-native-camera-roll/camera-roll”: β€œ^7.5.2”

if I comment out the following ( save to camera-roll part ), no error would show up, with / without β€œawait” keyword :

For example, I can console logged the uri : uri: file:///private/var/mobile/Containers/Data/Application/746DABFE-E230-4180- A986-8B607940951A/tmp/99E8D94D-1B69-467F-9269-97A92DAA7FE1.jpeg

I am following react-native-vision-camera documentation example code to implement react-native-cameraroll library, let me know if I need to put file://${photo.path} or just β€œuri” variable is fine ( I tried both, both can successfully saved the photo to album, but both got the same error ) : https://react-native-vision-camera.com/docs/guides/taking-photos Screenshot 2024-04-26 at 5 08 21 AM

My code :

( I keep adding try-catch block because of the errors, it got the Error 1 : Possible unhandled promise rejection ( id : 0 ): Error : Unknown error from a native module

and after I added try-catch block, it got Error 2: Error saving photo asset : Unknown error from a native module )

try{
            // await CameraRoll.saveAsset(`file://${photo.path}`, {
            await CameraRoll.saveAsset(uri, {
                type: 'photo',
            });

Whole code block looks like this :

const onTakePicture = async() =>{
      try {
          const photo = await camera.current?.takePhoto({
              // flash: 'auto'
          });
          // console.log("photo:", photo);
          const uri = photo.path;
          console.log("uri:", uri);
          try{
              // await CameraRoll.saveAsset(`file://${photo.path}`, {
              await CameraRoll.saveAsset(uri, {
                  type: 'photo',
              });
          }catch (error) {
              console.error('Error saving photo asset:', error);
              // Handle the error here, such as displaying an error message to the user or logging the error for further investigation
          }
      } catch (error) {
          console.error("Error while taking or saving photo:", error);
          // Handle the error here, such as displaying an error message to the user or logging the error for further investigation
      }
  };

Error 1 :

photo_2024-04-26 04 49 22

Error 2 : IMAGE 2024-04-26 05:14:54

Let me know if you need detailed Logcat or these are enough for the clues

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 18

Most upvoted comments

The additional permission prompt appears when the library tries to retrieve information about a saved asset. I managed to work around this with the following patch

diff --git a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
index 7a2e07f..cc1a49d 100644
--- a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
+++ b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
@@ -207,26 +207,8 @@ static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthor
       }
     } completionHandler:^(BOOL success, NSError *error) {
       if (success) {
-        PHFetchOptions *options = [PHFetchOptions new];
-        options.includeHiddenAssets = YES;
-        options.includeAllBurstAssets = YES;
-        options.fetchLimit = 1;
-        PHFetchResult<PHAsset *> *createdAsset = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholder.localIdentifier]
-                                                                                  options:options];
-        if (createdAsset.count < 1) {
-          reject(kErrorUnableToSave, nil, nil);
-          return;
-        }
-        NSDictionary *dictionary = [self convertAssetToDictionary:[createdAsset firstObject]
-                                                    includeAlbums:YES
-                                                  includeFilename:YES
-                                             includeFileExtension:YES
-                                                 includeImageSize:YES
-                                                  includeFileSize:YES
-                                          includePlayableDuration:YES
-                                                  includeLocation:YES
-                                                  includeSourceType:YES];
-        resolve(dictionary);
+        
+        resolve(nil);
       } else {
         reject(kErrorUnableToSave, nil, error);
       }

@cristinaITdeveloper Using PHOTO_LIBRARY instead of PHOTO_LIBRARY_ADD_ONLY may help you. Something likes below:

// Don't check PHOTO_LIBRARY_ADD_ONLY, use PHOTO_LIBRARY instead here.
const permission = PERMISSIONS.IOS.PHOTO_LIBRARY; 
const checkResult = await check(permission);
if (checkResult === RESULTS.DENIED || checkResult === RESULTS.BLOCKED) {
  const requestResult = await request(permission);
  if (requestResult === RESULTS.DENIED || requestResult === RESULTS.BLOCKED) {
    // Not granted! Do something else here.
    return;
  }
}
const filePath = "xxx";
await CameraRoll.saveAsset(filePath, { type: "photo" });

So, checking for permissions fixes the issue? We can close this then?