直播平台搭建源码,调用系统相册实现多选图片上传

发布时间 2023-06-06 14:12:09作者: 云豹科技-苏凌霄

直播平台搭建源码,调用系统相册实现多选图片上传

1、首先需要给webview的WebChromeClient设置以下代码,才可以实现h5与Android交互选取图片

 


private ValueCallback<Uri> mValueCallbackUri;
private ValueCallback<Uri[]> mValueCallbackUris;
this.setWebChromeClient(new WebChromeClient() {
            // For Android < 3.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                if (mValueCallbackUri != null) {
                    mValueCallbackUri.onReceiveValue(null);
                    mValueCallbackUri = null;
                }
                mValueCallbackUri = uploadMsg;
                showOptions();
            }
            //For Android 3.0 - 4.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                if (mValueCallbackUri != null) {
                    mValueCallbackUri.onReceiveValue(null);
                    mValueCallbackUri = null;
                }
                mValueCallbackUri = uploadMsg;
                showOptions();
            }
            // For Android > 4.1.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                if (mValueCallbackUri != null) {
                    mValueCallbackUri.onReceiveValue(null);
                    mValueCallbackUri = null;
                }
                mValueCallbackUri = uploadMsg;
                showOptions();
            }
            // For Android > 5.0支持多张上传
            @Override
            public boolean onShowFileChooser(WebView webView,
                                             ValueCallback<Uri[]> uploadMsg,
                                             FileChooserParams fileChooserParams) {
                if (mValueCallbackUris != null) {
                    mValueCallbackUris.onReceiveValue(null);
                    mValueCallbackUris = null;
                }
                mValueCallbackUris = uploadMsg;
                showOptions();
                return true;
            }
        });
 

2、点击h5页面设置的<input type="file" name="pic" accept="image/*">,就会调用到Android webview设置的openFileChooser或者onShowFileChooser

3、然后在openFileChooser或者onShowFileChooser中调用打开相册的代码即可

1)方法一:打开相册,并且只支持选择单张图片

 


Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 调用
activity.startActivityForResult(intent, TYPE_GALLERY);
 

 

或者

 


   Intent intent = new Intent();
   intent.setType("image/*");
   intent.setAction(Intent.ACTION_GET_CONTENT);
   activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), TYPE_GALLERY);
 

 

2)方法二:打开相册,并且支持选择多张图片(Android 5.0+才支持),只需在上述代码基础上增加以下代码即可

 


intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

 

 以上就是直播平台搭建源码,调用系统相册实现多选图片上传, 更多内容欢迎关注之后的文章