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

[image_picker]ImageSource.camera doesn't ask camera permission #13921

Closed
jimmyshiau opened this issue Jan 5, 2018 · 22 comments · Fixed by flutter/plugins#424
Closed

[image_picker]ImageSource.camera doesn't ask camera permission #13921

jimmyshiau opened this issue Jan 5, 2018 · 22 comments · Fixed by flutter/plugins#424
Assignees
Labels
package flutter/packages repository. See also p: labels.

Comments

@jimmyshiau
Copy link

jimmyshiau commented Jan 5, 2018

Steps to Reproduce

There are 2 buttons to pick image

void onTap$gallery() {
  pickImage(ImageSource.gallery, save);
}
void onTap$camera() {
  pickImage(ImageSource.camera, save);
}

Case 1: Tap gallery button then tap camera button
It will ask gallery permission when tap gallery button,
then tap camera button, it will show the following error

I/flutter (22048): PlatformException(error, Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.sec.android.app.camera/.Camera launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } clip={text/uri-list U:content://io.quire.app.imagepicker.provider/external_files/Pictures/Camera/IMG_20180105_093700401718347.jpg} (has extras) } from ProcessRecord{25efb3d 22048:io.quire.app/u0a208} (pid=22048, uid=10208) with revoked permission android.permission.CAMERA, null)

Case 2: Tap camera button then tap gallery button (before allow gallery permission)
It will show the following error

PlatformException(error, Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference, null)

then tap tap gallery button, it will show the following error

PlatformException(ALREADY_ACTIVE, Image picker is already active, null)

Flutter Doctor

[✓] Flutter (on Mac OS X 10.12.6 16G29, locale en-TW, channel master)
    • Flutter at /usr/local/flutter/flutter
    • Framework revision b6796f426f (3 days ago), 2018-01-02 08:24:21 +0100
    • Engine revision 7c126001d8
    • Tools Dart version 1.25.0-dev.11.0
    • Engine Dart version 2.0.0-edge.9e8a3e2d31621c1bdf6139d068e7898a2ac2ab5a

[✓] Android toolchain - develop for Android devices (Android SDK 26.0.3)
    • Android SDK at /Users/jimmy/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-26, build-tools 26.0.3
    • ANDROID_HOME = /Users/jimmy/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)

[✓] iOS toolchain - develop for iOS devices (Xcode 9.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.1, Build version 9B55
    • ios-deploy 1.9.2
    • CocoaPods version 1.3.1

[✓] Android Studio (version 3.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)

[✓] IntelliJ IDEA Community Edition (version 2017.2.2)
    • Flutter plugin version 19.1
    • Dart plugin version 172.3757.55

[✓] Connected devices
    • SM G930F                  • ce01171121b67a1a02                   • android-arm • Android 7.0 (API 24)
    • Android SDK built for x86 • emulator-5554                        • android-x86 • Android 5.1.1 (API 
@jimmyshiau
Copy link
Author

Workaround

case SOURCE_CAMERA:
          Intent intent = cameraModule.getCameraIntent(activity);
          if (intent == null) {
            ActivityCompat.requestPermissions(activity, new String[] {
              Manifest.permission.CAMERA,
              Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_CAMERA);
            pendingResult = null;
            return;
          }
          activity.startActivityForResult(intent, REQUEST_CODE_CAMERA);
//          activity.startActivityForResult(
//              cameraModule.getCameraIntent(activity), REQUEST_CODE_CAMERA);
          break;

@zoechi
Copy link
Contributor

zoechi commented Jan 31, 2018

Run into this after https://github.com/flutter/flutter/wiki/Updating-Flutter-projects-to-Gradle-4.1-and-Android-Studio-Gradle-plugin-3.0.1 and updating all plugins to the latest version.
It was working before.

Manually enabling the camera in Android made it work (Setttings > Apps > Your App > Permissions and enable Camera)

@ride4sun
Copy link

Pretty critical defect for us to get a release out.

@slightfoot
Copy link
Member

slightfoot commented Jan 31, 2018

I've been chatting with @zoechi on Gitter and at the bottom of this reply is his caught exception.

It looks like the image_picker plugin is using an external package for most of its functionality com.github.esafirm.android-image-picker:imagepicker:1.9.2@aar it looks like esafirm/android-image-picker is now on v1.12.0. I had a quick look, and it doesn't seem so but it might be worth updating for bug fixes.

I found that the android.media.action.IMAGE_CAPTURE Intent which the library uses now requires the Runtime Permission for the Camera after M as noted by the docs.

Note: if you app targets M and above and declares as using the CAMERA permission which is not granted, then attempting to use this action will result in a SecurityException.

So either the patching the external library or using an internal patch like @jimmyshiau above should resolve the issue.

I/flutter (12295): error: PlatformException(error, Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.google.android.GoogleCamera/com.android.camera.activity.CaptureActivity clip={text/uri-list U:content://com.lookatyou.lookatmybaby.imagepicker.provider/external_files/Pictures/Camera/IMG_20180131_150358263150491150871482.jpg} (has extras) } from ProcessRecord{1940294 12295:com.lookatyou.lookatmybaby/u0a362} (pid=12295, uid=10362) with revoked permission android.permission.CAMERA, null)

@sethladd
Copy link
Contributor

sethladd commented Feb 6, 2018

cc @collinjackson @mit-mit

@mit-mit
Copy link
Member

mit-mit commented Feb 8, 2018

Tagging milestone 3; this is a core plugin, and permissions are important to get right.

@collinjackson are you able to look into this?

@mit-mit mit-mit added this to the 3: Current Milestone milestone Feb 8, 2018
@mit-mit mit-mit added this to Plugins - Rich Ecosystem (collin, kroike, hans, mravn, sigurdm) in Milestone 3 Feb 8, 2018
@mtthsfrdrch
Copy link

mtthsfrdrch commented Feb 8, 2018

The android.media.action.IMAGE_CAPTURE intent, doesn't need the CAMERA permission. The intent just tells the system to start a camera app, which then should return an image.
The CAMERA permission is just needed when the Flutter app (or in this case the image_picker flutter plugin / the used android-image-picker library) wants to read from the camera hardware directly, which, after having a look, the library doesn't seem to do.
The CAMERA permission shouldn't be needed at all for a simple image picker. As far as i can see the used library declares it without need.

The documentation of the IMAGE_CAPTURE intent doesn't state that the CAMERA permission is needed (neither on M nor above), it just says that it will fail when the CAMERA permission is declared.

But besides all that: Is there a specific reason for using a library that comes with it's own custom image picker ui, whcih creates a dependency to another library and with that several dependencies to other libraries as well?
Instead the image_picker plugin could use the ACTION_GET_CONTENT intent to bring up the system document picker (f.e. filtered for the mime type image/*).
This would fit way better into the Android way of doing it and into the overall ecosystem as it f.e. also offers to use other apps like Dropbox/GDrive/Google Photos and others to pick images from (even cloud hosted ones).

@slightfoot
Copy link
Member

@mtthsfrdrch yea, that was my thoughts' as well. I've already started a media_picker plugin that lets you pick images or videos, or ask for a image or video to be captured.

But one point I would mention is that if the CAMERA permission is declared by other plugin's the app uses the image_picker could still fail but the exception condition is not caught, nor the request for permission. So if the image_picker plugin is to continue, it certainly is a required addition.

@ride4sun
Copy link

ride4sun commented Feb 8, 2018

But besides all that: Is there a specific reason for using a library that comes with it's own custom image picker ui, whcih creates a dependency to another library and with that several dependencies to other libraries as well?
Instead the image_picker plugin could use the ACTION_GET_CONTENT intent to bring up the system document picker (f.e. filtered for the mime type image/*).
This would fit way better into the Android way of doing it and into the overall ecosystem as it f.e. also offers to use other apps like Dropbox/GDrive/Google Photos and others to pick images from (even cloud hosted ones).

maybe the intent was to have the same component for IOS and Android?

@mtthsfrdrch
Copy link

@slightfoot fair enough, but i think in case both (starting other camera apps and approaching the camera api directly) get used, i'd rather prefer it crashing and with that being reported in crashlogs over silently failing ... or even worse: forcing the CAMERA permission on all the apps (and their users) that just want to pick an image (that would be an even bigger issue and for me personally a NO GO to use this plugin)

i'd Assume apps doing both are edge cases anyway and people messing with the camera api + its compatibility surely have time to build their own image_picker plugin as well 😉

the media_picker plugin would be nice 👍

@cbracken
Copy link
Member

cbracken commented Mar 2, 2018

@collinjackson assigning to you based on discussion

@arok
Copy link

arok commented Mar 14, 2018

This fork https://github.com/arok/plugins/tree/master/packages/image_picker should work as temporary workaround

@jonahwilliams
Copy link
Member

@collinjackson Are you currently working on this? I can push a short term fix to request the correct runtime permissions to get this working.

@collinjackson
Copy link
Contributor

@jonahwilliams thanks for #424, I can land that as soon as the tests pass.

Milestone 3 automation moved this from Plugins - Rich Ecosystem (collin, kroike, hans, mravn, sigurdm) to Done Mar 15, 2018
@jonahwilliams
Copy link
Member

I've just pushed a new version (0.3.1) to pub which has the fixes implemented.

@BasedMusa
Copy link

BasedMusa commented Mar 31, 2018

Installed version 0.4.0. It's giving me the same issue after I get past the permission dialogs.

@wrumble
Copy link

wrumble commented May 18, 2018

same for me on 0.4.1

@mit-mit
Copy link
Member

mit-mit commented May 22, 2018

@BarbaricGamester @wrumble if you are still having issues, can you file a new bug with repro steps, etc.?

@jonahwilliams
Copy link
Member

It would also be useful to know if this plugin works at 0.3.1 still or if this was a regression introduced after 0.4.0

@lifenautjoe
Copy link

Updated yesterday to image_picker: ^0.5.0+9 .

Getting a crashing app when trying to pick a file from the camera with

E/flutter (22546): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.huawei.camera/.ThirdCamera clip={text/uri-list U:content://social.openbook.app.dev.flutter.image_provider/external_files/Android/data/social.openbook.app.dev/files/Pictures/b1ed3fb1-cc0b-4117-a67f-c383328e8ff888974390309692903.jpg} (has extras) } from ProcessRecord{5a8c2a7 22546:social.openbook.app.dev/u0a127} (pid=22546, uid=10127) with revoked permission android.permission.CAMERA, null)

@arok
Copy link

arok commented Mar 20, 2019

@lifenautjoe pls see #29422

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 30, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package flutter/packages repository. See also p: labels.
Projects
None yet
Development

Successfully merging a pull request may close this issue.