bug: Camera cannot select picture from Redmi Gallery app
See original GitHub issueBug Report
Capacitor Version
npx cap doctor
output:
Installed Dependencies:
@capacitor/ios not installed
@capacitor/core 1.2.1
@capacitor/cli 1.2.1
@capacitor/android 1.2.1
Affected Platform(s)
- Android
- iOS
- Electron
- Web
Current Behavior
Using the Camera plugin on Android Redmi phones, when a picture is selected in the Gallery app, this error is thrown: Unable to process image. It doesn’t manifest itself with other apps (e.g. File Manager) and also there was no problem with the Cordova plugin which we used before upgrading to Capacitor.
Expected Behavior
Users are able to use the default Gallery app to select their pictures on Redmi phones.
Sample Code or Sample Application Repo
This is the config we use:
const options: CameraOptions = { quality: 30, resultType: CameraResultType.Uri, allowEditing: false, source: CameraSource.Photos, };
Reproduction Steps
Configure the Camera plugin to return a Uri and use the option to select a picture from gallery instead of taking a new photo.
Other Technical Details
- Phone: Redmi 6A/Redmi Note 7
- Android version: 8.1/9
- MIUI version: 10.3.6/10.3.6
Other Information
I suspect the bug concerns other MIUI devices, such as Xiaomi phones. I managed to track down the problem to saveTemporaryImage method in Camera.java. contentUri.getlastPathSegment tries to select the filename and a copy of the file is created in the cacheDir.
However, contentUri from the Gallery app has this format: content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20191013_191506.jpg
while the other apps return unescaped slashes in the path, e.g. content://com.mi.android.globalFileexplorer.myprovider/external_files/DCIM/Camera/IMG_20191013_191506.jpg
I created an easy solution for our app (using a random filename) but this is probably a symptom of a more serious issue and I don’t know what the proper fix would be. Is it enough to just unescape the path?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:30 (4 by maintainers)
Top GitHub Comments
As no one from the Capacitor team has replied, I’ll at least give you a workaround.
patch-camera.js
in the project root folder with this content:As you can see, this fix is very fragile as it relies on the specific
node_modules
path and Java source code. This ensures that a timestamp is used for the file name instead of the broken name from the Gallery app.postinstall
script to yourpackage.json
to patch the Capacitor source code.This automatically runs the fix after
npm install
and works perfectly with @capacitor/android": “^1.2.1”. We haven’t updated to 1.3 yet.On Samsung 21 I get the error: (via logcat in android studio)
This happens when (as a user) I attempt to add an image from the stock gallery.
WORKS with camera