|
@@ -15,6 +15,7 @@ import android.webkit.WebChromeClient;
|
|
|
import android.webkit.WebSettings;
|
|
|
import android.webkit.WebView;
|
|
|
import android.widget.FrameLayout;
|
|
|
+import android.widget.TextView;
|
|
|
|
|
|
import com.alibaba.android.arouter.facade.annotation.Route;
|
|
|
import com.cooleshow.base.BuildConfig;
|
|
@@ -23,6 +24,7 @@ import com.cooleshow.base.router.RouterPath;
|
|
|
import com.cooleshow.base.ui.activity.BaseActivity;
|
|
|
import com.cooleshow.base.utils.LogUtils;
|
|
|
import com.cooleshow.base.utils.PermissionUtils;
|
|
|
+import com.cooleshow.base.utils.ToastUtil;
|
|
|
import com.cooleshow.base.utils.Utils;
|
|
|
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
|
|
|
import com.cooleshow.teacher.widgets.helper.JsInterfaceHelper;
|
|
@@ -39,6 +41,7 @@ import com.luck.picture.lib.config.PictureMimeType;
|
|
|
import com.luck.picture.lib.entity.LocalMedia;
|
|
|
import com.luck.picture.lib.tools.PictureFileUtils;
|
|
|
import com.tbruyelle.rxpermissions3.RxPermissions;
|
|
|
+import com.yalantis.ucrop.util.FileUtils;
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
@@ -55,6 +58,7 @@ import static com.cooleshow.base.common.WebConstants.WEB_URL;
|
|
|
*/
|
|
|
@Route(path = RouterPath.WebCenter.ACTIVITY_HTML)
|
|
|
public class WebActivity extends BaseActivity<ActivityHtmlBinding> implements JsInterfaceHelper.OnJsMethodCallListener, View.OnClickListener {
|
|
|
+ private static final int REQUEST_CODE_FILE_CHOOSER = 500;
|
|
|
private WebView webView;
|
|
|
private String url;
|
|
|
private JsInterfaceHelper mInterfaceUtils;
|
|
@@ -260,11 +264,18 @@ public class WebActivity extends BaseActivity<ActivityHtmlBinding> implements Js
|
|
|
}
|
|
|
|
|
|
// Android > 5.0.1
|
|
|
+ @Override
|
|
|
public boolean onShowFileChooser(
|
|
|
WebView webView, ValueCallback<Uri[]> filePathCallback,
|
|
|
FileChooserParams fileChooserParams) {
|
|
|
- mUploadCallbackAboveL = filePathCallback;
|
|
|
- startPictureSelect();
|
|
|
+ LogUtils.i("pq", "fileChooserParams" + fileChooserParams.toString());
|
|
|
+ if (fileChooserParams.getAcceptTypes().length == 1 && fileChooserParams.getAcceptTypes()[0].equals("image/*")) {
|
|
|
+ mUploadCallbackAboveL = filePathCallback;
|
|
|
+ startPictureSelect();
|
|
|
+ } else {
|
|
|
+ mUploadCallbackAboveL = filePathCallback;
|
|
|
+ startFileSelect(fileChooserParams);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -320,6 +331,36 @@ public class WebActivity extends BaseActivity<ActivityHtmlBinding> implements Js
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
}
|
|
|
|
|
|
+ private void showFileChooser(String[] contentTypes) {
|
|
|
+
|
|
|
+ Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
+ intent1.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
+ if (contentTypes.length == 0) {
|
|
|
+ intent1.setType("*/*");
|
|
|
+ } else {
|
|
|
+ for (int i = 0; i < contentTypes.length; i++) {
|
|
|
+ intent1.setType(contentTypes[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Intent chooser = new Intent(Intent.ACTION_CHOOSER);
|
|
|
+ chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser");
|
|
|
+ chooser.putExtra(Intent.EXTRA_INTENT, intent1);
|
|
|
+// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{intent2});
|
|
|
+ startActivityForResult(chooser, REQUEST_CODE_FILE_CHOOSER);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startFileSelect(WebChromeClient.FileChooserParams fileChooserParams) {
|
|
|
+ new RxPermissions(this)
|
|
|
+ .request(Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
+ .subscribe(permission -> {
|
|
|
+ if (permission) {
|
|
|
+ showFileChooser(fileChooserParams.getAcceptTypes());
|
|
|
+ } else {
|
|
|
+ String tip = "上传文件需要储存卡读取权限,是否去设置?";
|
|
|
+ showCommonTipDialog(tip);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
private void startPictureSelect() {
|
|
|
new RxPermissions(this)
|
|
@@ -337,22 +378,27 @@ public class WebActivity extends BaseActivity<ActivityHtmlBinding> implements Js
|
|
|
.circleDimmedLayer(false)// 是否圆形裁剪 true or false
|
|
|
.forResult(PictureConfig.CHOOSE_REQUEST);
|
|
|
} else {
|
|
|
- CommonDialog dialog = new CommonDialog(this);
|
|
|
- dialog.show();
|
|
|
- dialog.setTitle("提示");
|
|
|
- dialog.setContent("上传照片需要相机权限,是否去设置?");
|
|
|
- dialog.setOnCancelClickListener(view1 -> {
|
|
|
- dialog.dismiss();
|
|
|
- });
|
|
|
-
|
|
|
- dialog.setOnConfirmClickListener(view1 -> {
|
|
|
- PermissionUtils.toSelfSetting(this);
|
|
|
- dialog.dismiss();
|
|
|
- });
|
|
|
+ String tip = "上传照片需要相机权限,是否去设置?";
|
|
|
+ showCommonTipDialog(tip);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private void showCommonTipDialog(String tip) {
|
|
|
+ CommonDialog dialog = new CommonDialog(this);
|
|
|
+ dialog.show();
|
|
|
+ dialog.setTitle("提示");
|
|
|
+ dialog.setContent(tip);
|
|
|
+ dialog.setOnCancelClickListener(view1 -> {
|
|
|
+ dialog.dismiss();
|
|
|
+ });
|
|
|
+
|
|
|
+ dialog.setOnConfirmClickListener(view1 -> {
|
|
|
+ PermissionUtils.toSelfSetting(this);
|
|
|
+ dialog.dismiss();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
@@ -367,6 +413,51 @@ public class WebActivity extends BaseActivity<ActivityHtmlBinding> implements Js
|
|
|
mUploadCallbackAboveL.onReceiveValue(new Uri[]{});
|
|
|
}
|
|
|
mUploadCallbackAboveL = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (requestCode == REQUEST_CODE_FILE_CHOOSER) {
|
|
|
+ Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
|
|
|
+// if (result == null && !TextUtils.isEmpty(mCameraFilePath)) {
|
|
|
+// // 看是否从相机返回
|
|
|
+// File cameraFile = new File(mCameraFilePath);
|
|
|
+// if (cameraFile.exists()) {
|
|
|
+// result = Uri.fromFile(cameraFile);
|
|
|
+// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
|
|
|
+// }
|
|
|
+// }
|
|
|
+ if (result != null) {
|
|
|
+ String path = FileUtils.getPath(this, result);
|
|
|
+ if (!TextUtils.isEmpty(path)) {
|
|
|
+ File f = new File(path);
|
|
|
+ if (f.exists() && f.isFile()) {
|
|
|
+ Uri newUri = Uri.fromFile(f);
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ if (mUploadCallbackAboveL != null) {
|
|
|
+ if (newUri != null) {
|
|
|
+ mUploadCallbackAboveL.onReceiveValue(new Uri[]{newUri});
|
|
|
+ mUploadCallbackAboveL = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ToastUtil.getInstance().show(WebActivity.this, "文件损坏或不存在,请重新选择");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ clearUploadMessage();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * webview没有选择文件也要传null,防止下次无法执行
|
|
|
+ */
|
|
|
+ private void clearUploadMessage() {
|
|
|
+ if (mUploadCallbackAboveL != null) {
|
|
|
+ mUploadCallbackAboveL.onReceiveValue(null);
|
|
|
+ mUploadCallbackAboveL = null;
|
|
|
}
|
|
|
}
|
|
|
}
|