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

Fixed the problem that webview cannot upload pictures #17657

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class CocosWebView extends WebView {

private int mViewTag;
private String mJSScheme;
private ValueCallback<Uri> mValueCallback;
private ValueCallback<Uri[]> mFilePathCallback;


public CocosWebView(Context context) {
this(context, -1);
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don`t need to support API level lower than anroid5.0.

Android 6.0 (API 23), which brought another significant change: dynamic permission management. This update impacted how apps handle permission-related callbacks, such as onPermissionRequest and onGeolocationPermissionsShowPrompt, allowing users to grant or deny permissions in a more granular way, rather than at install time. This shift gave users more control over their data and privacy.

public void openFileChooser(ValueCallback<Uri> 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<Uri> valueCallback, String acceptType, String capture) {
mValueCallback = valueCallback;
openImageChooserActivity();
}

// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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) {
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +48,7 @@ public class CocosWebViewHelper {

private static SparseArray<CocosWebView> webViews;
private static int viewTag = 0;
public final static int WEBVIEW_IMAGE_CHOOSER_REQUEST_CODE = 1000000;

public CocosWebViewHelper(FrameLayout layout) {

Expand Down Expand Up @@ -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);
}
}
});
}
}
Loading