简体   繁体   中英

Cannot retrieve file, error: The file couldn’t be opened because you don’t have permission to view it

The app that I am working has been working perfectly fine. I am able to choose a picture from the library and upload to Firebase. However, it suddenly does not allow me to upload the file from photo library. From the simulator, there is no problem. Only on my phone, I would receive that error.

the error that I received is

Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_8982.JPG” couldn’t be opened because you don’t have permission to view it." 
UserInfo={NSURL=file:///var/mobile/Media/DCIM/138APPLE/IMG_8982.JPG,
NSFilePath=/var/mobile/Media/DCIM/138APPLE/IMG_8982.JPG,
NSUnderlyingError=0x12ed877f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

This is weird as I have been doing it all the while but it suddenly stopped working today.

This is how my code looks like.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    // Clear data if picture is repicked

    imageLocalURL = nil
    imageData = nil

    imageSelected = info[UIImagePickerControllerOriginalImage] as? UIImage

    if info[UIImagePickerControllerReferenceURL] != nil {

        // Image is selected from Photo Library
        referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL

    } else {

        // Image is taken from camera
        imageData = UIImageJPEGRepresentation(imageSelected!, 1.0)

    }

    transportImage.image = imageSelected
    dismissViewControllerAnimated(true, completion: nil)

}


func saveDetails() {

    if imageSelected != nil {

           if referenceURL != nil {

                // Image is from photo library

                let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceURL! as NSURL], options: nil)
                let asset = assets.firstObject
                let checkValidation = NSFileManager.defaultManager()
                asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
                    self.imageLocalURL = contentEditingInput?.fullSizeImageURL

                    // I did this to check if there is a local URL and if the file exist, but it returns false
                    print(self.imageLocalURL)
                    print(checkValidation.fileExistsAtPath("\(self.imageLocalURL)"))

                    self.uploadTask = storageRef.child(filePath).putFile(self.imageLocalURL!, metadata: nil) { (metadata, error) in
                        if let error = error {
                            print("Error uploading: \(error.description)")
                            return

                        }
                    }

            } else if imageData != nil {

                let imageLocalData = UIImageJPEGRepresentation(imageSelected!, 0.8)
                let metadata = FIRStorageMetadata()
                metadata.contentType = "image/jpeg"
                uploadTask = storageRef.child(filePath).putData(imageLocalData!, metadata: metadata) { (metadata, error) in
                        if let error = error {
                            print("Error uploading: \(error)")
                            return
                        }

                }

                UIImageWriteToSavedPhotosAlbum(imageSelected!, nil, nil, nil)

            }

If the image is taken from the camera, there is no problem. If the image is taken from photo library, I receive that error.

I searched around and I have tried Option+Clean the build. I went to Projects->derived data-> delete I checked the compiler is default.

Could anyone assist me with this issue?

I'm guessing you're getting denied access once you're outside the scope of didFinishPickingMediaWithInfo.

Maybe you can pull the image from it, instead of keeping the URL?

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

Edit:

Sorry, didn't notice you were in Swift, and that you already pull the image. Still, my suggestion stands. Keep the image around instead of the URL, and you should be fine.

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