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..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 @@ -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,33 @@ 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) { + 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(); + 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)}; + } + } + 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..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 @@ -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("*/*"); + // 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,18 @@ 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) { + if(files != null && files.length > 0) { + mValueCallback.onReceiveValue(files[0]); + } else { + mValueCallback.onReceiveValue(null); + } + 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); + } + } + }); + } }