Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(camera) Multiple selection not working on some Android devices #823

Closed
dragermrb opened this issue Feb 16, 2022 · 10 comments
Closed

(camera) Multiple selection not working on some Android devices #823

dragermrb opened this issue Feb 16, 2022 · 10 comments

Comments

@dragermrb
Copy link

Bug Report

On android, method pickImages() (and pickImage() too) uses Intent.ACTION_PICK with extra data Intent.EXTRA_ALLOW_MULTIPLE set to true, but ACTION_PICK is not documented to support EXTRA_ALLOW_MULTIPLE.

Specifically:

Selecting multiple photos on some devices (Oppo/Realme for example) doesn't work.

Switch Intent.ACTION_PICK to Intent.ACTION_OPEN_DOCUMENT can solve the problem.

Plugin(s)

  • Camera

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 3.4.1
  @capacitor/core: 3.4.1
  @capacitor/android: 3.4.1
  @capacitor/ios: 3.4.1

Installed Dependencies:

  @capacitor/cli: 3.2.4
  @capacitor/core: 3.4.1
  @capacitor/ios: 3.4.1
  @capacitor/android: 3.4.1

Platform(s)

  • Android

Current Behavior

Selecting multiple photos on some devices (Oppo/Realme for example) doesn't work.

Expected Behavior

Selecting multiple photos on some devices (Oppo/Realme for example) does work.

Additional Context

@HarelM
Copy link

HarelM commented Oct 31, 2022

@dragermrb did you solve this?
I'm running into the same issue where I open the gallery on my samsung phone, select images and I don't get anything back.
I also get ActivityResult{resultCode=RESULT_CANCELED, data=null} from the callback so no data is sent to the app and there's an error of No images picked.
I can easily reproduce this in my app... :-(

@HarelM
Copy link

HarelM commented Oct 31, 2022

I changed the code to use Intent.ACTION_OPEN_DOCUMENT and it does solves the issue, but it doesn't allow the user to select the gallery app, which is not a great experience, IMHO.
I've found this SO question that talks about launchMode requiring singleTop. it also seems to solve this issue as well.
https://stackoverflow.com/questions/5297361/android-gallery-intent-returning-resultcode-result-canceled
https://stackoverflow.com/questions/3354955/onactivityresult-called-prematurely

@dragermrb
Copy link
Author

Hi @HarelM

In my case I have chosen to use a hook that changes Intent.ACTION_PICK to Intent.ACTION_OPEN_DOCUMENT. It is not the best option, but it is the only one that allows us to select multiple images on all devices.

@HarelM
Copy link

HarelM commented Nov 1, 2022

I'm comparing the code in this plugin to cordova's camera plugin and I don't see Intent.ACTION_OPEN_DOCUMENT there, but only Intent.ACTION_PICK.
I'm not sure I fully understand what's going on there, but it seems that the image is stored in a pre-defined location and the camera is writing the image there instead of relaying on the return value of the intent, I think, I'm not sure.
https://github.com/apache/cordova-plugin-camera/blob/e9db20e381e0e60baa14664543271d094f79e91c/src/android/CameraLauncher.java#L371

Currently I'm using patch-package to do what you said - change the intent in the code, but I don't like patch-package because I won't be able to easily update to more recent versions of this plugin...

I'm not sure what the right solution is in this case... :-/

@dragermrb
Copy link
Author

You can use hooks to apply patch after capacitor sync. See docs. You can define them in ionic.config.json or packages.json

On file ionic.config.json add hooks at root level:

{
  "name": "My app",
  "integrations": {
    "capacitor": {}
  },
  "type": "angular",
  "hooks": {
    "capacitor:sync:after": "./hooks/fix-android-image-picker.js"
  }
}

Then, create hooks/fix-android-image-picker.js file:

#!/usr/bin/env node

fs = require('fs');

// Update @capacitor/camera plugin to use Intent.ACTION_OPEN_DOCUMENT

var targetFile = 'node_modules/@capacitor/camera/android/src/main/java/com/capacitorjs/plugins/camera/CameraPlugin.java';
var pattern = 'Intent.ACTION_PICK';
var replacement = 'Intent.ACTION_OPEN_DOCUMENT';

module.exports = function (ctx) {
  console.log('[Hook]: Fix android image picker');

  if (ctx.build.platform === 'android') {
    if (fileExists(targetFile)) {
      console.log('  - - Fixing file', targetFile);
      replaceInFile(targetFile, pattern, replacement);
      console.log('  - - - File fixed!');
    }
  } else {
    console.log('  - - Skipped!');
  }
};

// function to test if file exists
function fileExists(filePath) {
  try {
    return fs.statSync(filePath).isFile();
  } catch (err) {
    return false;
  }
}

// function to replace pattern in file
function replaceInFile(filePath, pattern, replacement) {
  var file = fs.readFileSync(filePath, 'utf8');
  var result = file.replace(pattern, replacement);
  fs.writeFileSync(filePath, result, 'utf8');
}

@HarelM
Copy link

HarelM commented Nov 1, 2022

@dragermrb THANKS!! this is super useful! (although a bit more complicated than patch-package but seems more robust)

@HarelM

This comment was marked as abuse.

@HarelM
Copy link

HarelM commented May 30, 2023

I've opened a PR to push this fix into this repo: #1549
I would love to have it reviewed if possible.

@jcesarmobile
Copy link
Member

The Camera plugin version 6 now uses the Android Photo Picker instead of using Intent.ACTION_PICK, that should fix this problem.

Copy link

ionitron-bot bot commented Dec 28, 2023

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Dec 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants