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

Permissions Broken For Android 29 (Q) on any openPicker/openCamera calls due to Environment API Deprecations #1110

Closed
phil-flyclops opened this issue Sep 18, 2019 · 7 comments

Comments

@phil-flyclops
Copy link
Contributor

Version

  • react-native-image-crop-picker v0.25.0
  • react-native v0.59.10

Platform

  • Android

Expected behaviour

On a device running Android SDK 29 a call to ImagePicker.openCamera/ImagePicker.openPicker should prompt for permissions and then on acceptance open the picker or camera

Actual behaviour

Even when the user grants permissions the picker or camera will eventually fail once an attempt is made to get a storage directory

Steps to reproduce

  1. Have Phone Running Android SDK 29

  2. Use openCamera or openPicker and grant required permissions

  3. openCamera fails immediately and openPicker fails at cropping phase

Attachments

For openCamera the following error appears with no stack trace:
E/image-crop-picker: Promise rejected. Permission denied

For openPicker there is a full stack trace

2019-09-18 17:39:08.417 19436-19436/com.flyclops.domino.android E/TransformImageView: onFailure: setImageUri
    java.io.FileNotFoundException: open failed: EACCES (Permission denied)
        at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315)
        at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1498)
        at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1338)
        at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1286)
        at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:100)
        at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:44)
        at android.os.AsyncTask$3.call(AsyncTask.java:378)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
2019-09-18 17:39:08.467 19436-19436/com.flyclops.domino.android E/image-crop-picker: Promise rejected. Cannot find image data

I discovered the general Android 29 behavior here
https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android`

which led to a solution suggestion here
https://stackoverflow.com/questions/56468539/getexternalstoragepublicdirectory-deprecated-in-android-q

More info in deprecation here https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String)

I am very willing to work on adding support, I'm reaching out mostly for guidance on the process for doing so.

Love react-native-image-crop-picker? Please consider supporting our collective:
👉 https://opencollective.com/react-native-image-crop-picker/donate

@phil-flyclops phil-flyclops changed the title Permissions Broken For Android 29 (Q) on any openPicker/openCamera calls Permissions Broken For Android 29 (Q) on any openPicker/openCamera calls due to Environment API Deprecations Sep 18, 2019
@phil-flyclops
Copy link
Contributor Author

Can anybody give guidance on how to get the project set up for android development? If I just open the .iml I get issues with the library plugin not being found, and if I explicitly put in a gradle dependency it fails to find the react-native library. I'm definitely down to try and fix this for the community, I'm just struggling to get up to speed

phil-flyclops added a commit to Flyclops/react-native-image-crop-picker that referenced this issue Sep 25, 2019
phil-flyclops added a commit to Flyclops/react-native-image-crop-picker that referenced this issue Sep 25, 2019
phil-flyclops added a commit to Flyclops/react-native-image-crop-picker that referenced this issue Sep 25, 2019
@atomontage04
Copy link

um, the documentation says:

If you are using react-native >= 0.60 use react-native-image-crop-picker version >= 0.25.0. Otherwise use version < 0.25.0.

and your versions are:

react-native-image-crop-picker v0.25.0
react-native v0.59.10

I'm not sure whether this is the reason but who knows.

@phil-flyclops
Copy link
Contributor Author

I've Jetified react native 59.10 for compatibility between the two. Everything works without a hitch using targetSdkVersion 28, but as soon as that is flipped to targetSdkVersion 29 the filesystem calls fail with perm problems due to how Android deprecated their Environment apis in a breaking way. At this point in React Native 61 the targetSdkVersion is still set to 28 so almost all users will not be affected yet, but this library is essentially a time bomb for the day that react native finally upgrades

@EmpireJones
Copy link

Possibly related (fixed permissions issues for me):

Per the second link:

To give users more control over their files and to limit file clutter, apps targeting Android 10 (API level 29) and higher are given scoped access into an external storage device, or scoped storage, by default. Such apps can see only their app-specific directory—accessed using Context.getExternalFilesDir()—and specific types of media.

Workaround:
<application android:requestLegacyExternalStorage="true" ... >

I believe the real solution would be to change the location where files are being stored to use Context.getExternalFilesDir(); this pull request might fix it:
#1127

@taschik
Copy link

taschik commented Mar 26, 2020

Thanks for the workaround! Worked like a charm!

@paramaggarwal
Copy link

This is the section of code that needs to be changed:

File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
if (!path.exists() && !path.isDirectory()) {
path.mkdirs();
}
File image = File.createTempFile(imageFileName, ".jpg", path);

related: #1260 #1127

ivpusic added a commit that referenced this issue May 1, 2020
* Switch Android SDK 29 deprecated methods to alternatives (#1110)

Android SDK 29 deprecated the following method for getting external directories
that we use repeatedly.
https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String)

I converted the various use cases to use the suggested alternative in
the Context API.
https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

* Updated podspec

* Added iOS 13 DarkMode for picker (#1129)

* Update README.md

* version bump

* Removed isMovingToParentViewController so scroll to bottom works (#1137)

* Fix deprecated RCTImageLoader.h import (#1142)

* upgrade example project to react-native 0.61

* version bump

* Update README.md

* Fix crash after video recording in ios13 & xcode11 (#1145)

* Fix crash after video recording in ios13 & xcode11

UI-related code should run in main thread, as exception says:

`Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread'`

* Typo fix

* Fix xCode warnings about main thread execution (#1146)

This kind of warnings also presents in output console if Main Thread Checker is active

* ignore iml files

* version bump

* Fix typo (#1150)

Changed the word "noticable" to "noticeable".

* update type (#1200)

* CocoaPods - Properly Interpolate Tag Version (#1174)

Why?
* Ruby uses `"` quotes when interpolation is required.  Using `'` will mean `v#{version}` 
  is used directly rather than providing the version number.

Changes:
1. User Ruby string interpolation when setting the tag version for the source.

* small typing improvements

* version bump

* fix typings for writeTempFile

* version bump

* (ios) changing images sort order

* upgrade react-native-cli

* using local QBImagePicker as podspec

* Update README.md (#1265)

Clarify that avoidEmptySpaceAroundImage option is (ios only)

* testing with sdk 29

* update yarn.lock

* version bump

Co-authored-by: Phil Simmons <phil@flyclops.com>
Co-authored-by: Yury Korzun <yury@flyclops.com>
Co-authored-by: cr1s3c <yannre1210@gmail.com>
Co-authored-by: Filipe Merker <filipesmerker@gmail.com>
Co-authored-by: Artur Eshenbrener <strate@yandex.ru>
Co-authored-by: Suraneti Rodsuwan <suraneti.rod@gmail.com>
Co-authored-by: mikunimaru <43168745+mikunimaru@users.noreply.github.com>
Co-authored-by: Peter Lucak <login@peterlucak.com>
Co-authored-by: Ivan Pusic <ivpusic@users.noreply.github.com>
Co-authored-by: Mujtaba F. Radhi <al.mujtaba@hotmail.com>
@ivpusic
Copy link
Owner

ivpusic commented May 1, 2020

please try https://github.com/ivpusic/react-native-image-crop-picker/releases/tag/v0.30.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants