gree / unity-webview

zlib License
2.24k stars 684 forks source link

The file selection for input file is not working. #1022

Closed zhanglubin12345 closed 8 months ago

zhanglubin12345 commented 8 months ago

I tried switching to UniWebView and found it functional. After making the following adjustments to the code, input file is also working now.

               public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                    // cf. https://github.com/googlearchive/chromium-webview-samples/blob/master/input-file-example/app/src/main/java/inputfilesample/android/chrome/google/com/inputfilesample/MainFragment.java

+                    if (mFilePathCallback != null) {
+                        mFilePathCallback.onReceiveValue(null);
+                    }
+                    mFilePathCallback = filePathCallback;
-                    MyUnitySendMessage(gameObject, "RequestFileChooserPermissions", "");

+                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
+                    intent.addCategory(Intent.CATEGORY_OPENABLE);
+                    intent.setType("image/*");
+                    startActivityForResult(intent, INPUT_FILE_REQUEST_CODE);
+                    return true;
                }
KojiNakamaru commented 8 months ago

The plugin should work without any modification If you correctly follow the instruction described in https://github.com/gree/unity-webview/tree/1235da5a7461ccc83dd1274caba45c203b9aa37d#file-input-field .

zhanglubin12345 commented 8 months ago

@KojiNakamaru I've certainly tried all of these, but none of them worked properly. Therefore, I modified the plugin's code to make it work correctly.

KojiNakamaru commented 8 months ago

Please provide your environment information such as Unity version, Android version, device name, etc.

KojiNakamaru commented 8 months ago

I put an adjusted sample app under https://github.com/gree/unity-webview/tree/example-for-1022/sample, which can be directly built with Unity 2020.3.48f1. This works well at least on OSCAL Pad8 (Android 11) and Pixel 6a (Android 13).

zhanglubin12345 commented 8 months ago

@KojiNakamaru I have tested with both Unity 2020.3.40f1 and Unity 2022.3.14f1. My test device runs on Android 11. However, other players on our team are also unable to use this upload feature, so it seems to be a common issue.

Typically, when facing this issue, it's because the permissions are not set in the AndroidManifest.xml. However, even after setting the AndroidManifest.xml, unity-webview still doesn't work. Therefore, I tried developing a new AAR file myself, which simply displays a WebView, and indeed, the AAR file I developed works.

Next, I tried another commonly used plugin, UniWebView, and found that UniWebView is also working correctly. This confirms that unity-webview has an issue. I tested the sample you provided in the master branch, but it also doesn't work.

KojiNakamaru commented 8 months ago

Please try https://github.com/gree/unity-webview/tree/example-for-1022/sample at first, as is. If it doesn't work then we can investigate the issue further based on the same code.

FYI, the former code of this plugin performed almost the same thing you wrote. The current code was introduced because permission requests didn't work well for some cases.

zhanglubin12345 commented 8 months ago

@KojiNakamaru Yes, it works

zhanglubin12345 commented 8 months ago

I have tried the sample from the master branch, and it doesn't work. What are the differences between this branch and the master branch?

KojiNakamaru commented 8 months ago

The changes from the master branch can be found at https://github.com/gree/unity-webview/compare/1235da5...06d8c41 .

zhanglubin12345 commented 8 months ago

I don't know why importing the latest version of unity-webview.unitypackage doesn't work, but it runs successfully with the modifications I mentioned above. Thank you for your response. This has taken up too much of my time, so I will have to use the version with my own modifications for now.