From daa7593ac14abc9e31808c3e7db7500b021945bc Mon Sep 17 00:00:00 2001 From: qgh <> Date: Fri, 20 Sep 2024 14:18:46 +0800 Subject: [PATCH 1/3] Fixed the problem that webview cannot upload pictures --- .../java/src/com/cocos/lib/CocosActivity.java | 29 ++++++++++ .../java/src/com/cocos/lib/CocosWebView.java | 54 ++++++++++++++++++- .../src/com/cocos/lib/CocosWebViewHelper.java | 14 +++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java index 55e93a24ba9..59c7baab92f 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java @@ -24,10 +24,13 @@ package com.cocos.lib; +import android.content.ClipData; +import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.media.AudioManager; +import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; @@ -250,4 +253,30 @@ private void onLoadNativeLibraries() { e.printStackTrace(); } } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, resultCode, intent); + if (requestCode >= CocosWebViewHelper.WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE) { + Uri[] files = null; + if (intent != null) { + String dataString = intent.getDataString(); + ClipData clipData = intent.getClipData(); + if (clipData != null) { + files = new Uri[clipData.getItemCount()]; + for (int i = 0; i < clipData.getItemCount(); i++) { + ClipData.Item item = clipData.getItemAt(i); + files[i] = item.getUri(); + } + } + if (dataString != null) { + files = new Uri[]{Uri.parse(dataString)}; + } + } + if(files != null) { + CocosWebViewHelper.onChooseFileResult(requestCode, files); + } + } + + } } diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java index c752c889bf2..59cecd55293 100755 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java @@ -27,12 +27,14 @@ of this software and associated engine source code (the "Software"), a limited, import android.annotation.SuppressLint; import android.content.Context; +import android.content.Intent; +import android.net.Uri; import android.util.Log; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; - +import android.webkit.ValueCallback; import java.lang.reflect.Method; import java.net.URI; import java.util.concurrent.CountDownLatch; @@ -62,6 +64,9 @@ public class CocosWebView extends WebView { private int mViewTag; private String mJSScheme; + private ValueCallback mValueCallback; + private ValueCallback mFilePathCallback; + public CocosWebView(Context context) { this(context, -1); @@ -90,7 +95,42 @@ public CocosWebView(Context context, int viewTag) { } this.setWebViewClient(new Cocos2dxWebViewClient()); - this.setWebChromeClient(new WebChromeClient()); + this.setWebChromeClient(new WebChromeClient() { + // For Android < 3.0 + public void openFileChooser(ValueCallback valueCallback) { + mValueCallback = valueCallback; + openImageChooserActivity(); + } + + // For Android >= 3.0 + public void openFileChooser(ValueCallback valueCallback, String acceptType) { + mValueCallback = valueCallback; + openImageChooserActivity(); + } + + // For Android >= 4.1 + public void openFileChooser(ValueCallback valueCallback, String acceptType, String capture) { + mValueCallback = valueCallback; + openImageChooserActivity(); + } + + // For Android >= 5.0 + @Override + public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { + mFilePathCallback = filePathCallback; + openImageChooserActivity(); + return true; + } + }); + } + + private void openImageChooserActivity() { + Intent i = new Intent(Intent.ACTION_GET_CONTENT); + i.addCategory(Intent.CATEGORY_OPENABLE); + i.setType("image/*"); + // requestCode = WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE + this.mViewTag + // Because webview may have multiple instances. + GlobalObject.getActivity().startActivityForResult(Intent.createChooser(i, "Image Chooser"), CocosWebViewHelper.WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE + this.mViewTag); } public void setJavascriptInterfaceScheme(String scheme) { @@ -170,4 +210,14 @@ public void setWebViewRect(int left, int top, int maxWidth, int maxHeight) { layoutParams.height = maxHeight; this.setLayoutParams(layoutParams); } + + public void onChooseFileResult(final Uri[] files) { + if (mFilePathCallback != null) { + mFilePathCallback.onReceiveValue(files); + mFilePathCallback = null; + } else if (mValueCallback != null && files.length > 0) { + mValueCallback.onReceiveValue(files[0]); + mValueCallback = null; + } + } } diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebViewHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebViewHelper.java index adf17aa99c0..f9ad80b4ed2 100755 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebViewHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebViewHelper.java @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal package com.cocos.lib; import android.graphics.Color; +import android.net.Uri; import android.os.Handler; import android.os.Looper; import android.util.SparseArray; @@ -47,6 +48,7 @@ public class CocosWebViewHelper { private static SparseArray webViews; private static int viewTag = 0; + public final static int WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE = 1000000; public CocosWebViewHelper(FrameLayout layout) { @@ -316,4 +318,16 @@ public void run() { } }); } + public static void onChooseFileResult(final int requestCode, final Uri[] files) { + GlobalObject.runOnUiThread(new Runnable() { + @Override + public void run() { + // requestCode = WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE + webviewTag + CocosWebView webView = webViews.get(requestCode - WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE); + if (webView != null) { + webView.onChooseFileResult(files); + } + } + }); + } } From 9883ebd4e6c505ff2df1aabbf20d29bea8c066c1 Mon Sep 17 00:00:00 2001 From: qgh <> Date: Fri, 20 Sep 2024 15:23:44 +0800 Subject: [PATCH 2/3] Incorrect handling when uploading pictures in webview --- .../android/java/src/com/cocos/lib/CocosActivity.java | 9 ++++++--- .../android/java/src/com/cocos/lib/CocosWebView.java | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java index 59c7baab92f..0f0d103c951 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java @@ -258,6 +258,11 @@ private void onLoadNativeLibraries() { protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode >= CocosWebViewHelper.WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE) { + if(resultCode != RESULT_OK) { + // If we do not select a picture. + CocosWebViewHelper.onChooseFileResult(requestCode, null); + return; + } Uri[] files = null; if (intent != null) { String dataString = intent.getDataString(); @@ -273,9 +278,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent) files = new Uri[]{Uri.parse(dataString)}; } } - if(files != null) { - CocosWebViewHelper.onChooseFileResult(requestCode, files); - } + CocosWebViewHelper.onChooseFileResult(requestCode, files); } } diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java index 59cecd55293..58769142e41 100755 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java @@ -215,8 +215,12 @@ public void onChooseFileResult(final Uri[] files) { if (mFilePathCallback != null) { mFilePathCallback.onReceiveValue(files); mFilePathCallback = null; - } else if (mValueCallback != null && files.length > 0) { - mValueCallback.onReceiveValue(files[0]); + } else if (mValueCallback != null) { + if(files != null && files.length > 0) { + mValueCallback.onReceiveValue(files[0]); + } else { + mValueCallback.onReceiveValue(null); + } mValueCallback = null; } } From f01b72d872b4ae574be4474237f97003a1688e43 Mon Sep 17 00:00:00 2001 From: qgh <> Date: Fri, 20 Sep 2024 17:31:49 +0800 Subject: [PATCH 3/3] File chooser for all file types --- .../platform/android/java/src/com/cocos/lib/CocosWebView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java index 58769142e41..0cc41b0ccdc 100755 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosWebView.java @@ -127,7 +127,7 @@ public boolean onShowFileChooser(WebView webView, ValueCallback filePathC private void openImageChooserActivity() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); + i.setType("*/*"); // requestCode = WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE + this.mViewTag // Because webview may have multiple instances. GlobalObject.getActivity().startActivityForResult(Intent.createChooser(i, "Image Chooser"), CocosWebViewHelper.WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE + this.mViewTag);