Camera/Android: Missing permission/no EXIF when using CameraSource.Photos
Description of the problem:
When using Camera.getPhoto with CameraSource.Photos on Android I noticed the following error message in Logcat:
02-22 11:10:38.603 6195-6195/app.prpl.dev E/Capacitor: Error loading exif data from image
java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20190209_142015.jpg (Permission denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:200)
at java.io.FileInputStream.<init>(FileInputStream.java:150)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at android.media.ExifInterface.<init>(ExifInterface.java:1334)
at com.getcapacitor.plugin.camera.ImageUtils.getExifData(ImageUtils.java:124)
at com.getcapacitor.plugin.Camera.returnResult(Camera.java:323)
at com.getcapacitor.plugin.Camera.processPickedImage(Camera.java:267)
at com.getcapacitor.plugin.Camera.handleOnActivityResult(Camera.java:450)
Reading the EXIF data from the file requires the READ_EXTERNAL_STORAGE permission, but that permission isn't checked when getPhoto is called with CameraSource.Photos (It only gets checked if it is called with CameraSource.Camera/CameraSource.Prompt and saveToGallery: true). Because the exeption is thrown by ExifInterface, but caught in getExifData in camera/ImageUtils.java, the camera plugin doesn't crash, but the exif data will be missing.
Affected platform
- [x] Android
- [ ] iOS
- [ ] electron
- [ ] web
OS of the development machine
- [x] Windows
- [ ] macOS
- [ ] linux
Other information:
Capacitor version: 1.0.0-beta.17 node version: v10.13.0 npm version: 6.2.0
Steps to reproduce:
Call Camera.getPhoto({source: CameraSource.Photos})
@WIStudent I have the same problem. Did you find any solution?
@lucascardoso The user could set the storage permission in the android settings as a workaround, but that wouldn't be really user-friendly.
The actual fix needs to happen in capacitor's camera implementation. It needs to check and require the storage permission the same way it does for the camera permission when taking a picture. I did look into the code but didn't have time yet to create a pull request.
@WIStudent
In android 8.1 that I'm testing it requests the permissions of the user when calling the CameraSource.Camera or CameraSource.Photos
With the version of Android 8.1 (Oreo) nothing works for me. but with the version of Android 5 (Lollipop) works everything right.
Take photo works on Android 5.1 but not on android 8.1
try {
const image = await Camera.getPhoto({
quality: 100,
resultType: CameraResultType.Uri,
source: CameraSource.Camera,
saveToGallery: true,
allowEditing: true,
width: size,
height: size
})
} catch (e) {
console.log("Erro getPhoto: ", e);
}
In the AndroidStudio log this appears
E/Capacitor/Plugin/Console: Erro getPhoto: {"message":"No image picked"}
In the same way this error happens using CameraSource.Photos with android 8.1
@lucascardoso I think this might be a different issue. It could be releated to the allowEditing option, I haven't yet used it myself.
@WIStudent
you're right, it's an allowEditing problem.
I tested without allowEditing to take photo on android 8.1 and it worked
Just to add to this, I have also experienced the issue with allowEditing. My use case does not currently need the option so removing it works fine. I still think it would be a good call to raise this as an issue.
@jcesarmobile should I create a bug report or can we turn this issue into one ? I can reproduce the exact issue described by @lucascardoso