yasirkula / UnityNativeFilePicker

A native Unity plugin to import/export files from/to various document providers on Android & iOS
MIT License
277 stars 30 forks source link

READ_EXTERNAL_STORAGE permission is mandatory to import a file on Android < 13, even if we don't want to use external storage. #57

Closed tsoen closed 2 weeks ago

tsoen commented 3 weeks ago

Description of the bug

We force the permission READ_EXTERNAL_STORAGE to be removed from our manifest for security reason, and we don't need to read the external storage. When we try to import a file (NativeFilePicker.PickFile) on a Android 12- device, the picker doesn't open because the RequestPermission method returns Permission.Denied.

The Picker works fine on Android 13+ devices and we are able to import files from the internal storage.

I think the issue comes from your NativeFilePicker.java class: image

Even if I comment the permission check in C# like this: image

I can't check if it would actually work without the READ_EXTERNAL_STORAGE in the manifest, because your Java PickFiles method checks again for the permisison : image

Reproduction steps

Try to PickFile on Android 12- with READ_EXTERNAL_STORAGE removed from AndroidManifest like this: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove" />

Platform specs

Please provide the following info if this is a Unity 3D repository.

Additional info

No error message. CheckPermission returns Permission.Denied.

yasirkula commented 3 weeks ago

In all of my native plugins, I enforce the permissions to avoid edge cases. I've seen countless edge cases where something doesn't work on a very specific configuration (Xiaomis on landscape orientation, Huaweis with MIUI, etc.) and some of those edge cases are caused by missing permissions (although the permission is supposed to be optional). Sometimes, even the official documentation says something and that turns out to be false on certain devices for unknown reasons. So I take all necessary precautions to eliminate the edge cases as much as possible.

I add configurable parameters for certain native stuff (they are mostly "use at your own risk" parameters) and I may add a similar parameter here, too.

yasirkula commented 2 weeks ago

I've implemented the flag. You can call this in Awake to enable it:

#if UNITY_ANDROID
using( AndroidJavaClass ajc = new AndroidJavaClass( "com.yasirkula.unity.NativeFilePicker" ) )
    ajc.SetStatic<bool>( "PermissionFreeMode", true );
#endif