android下webview实现H5 html标签 input file类型文件多选 multiple 属性的支持

android下webview实现H5 html标签 input file类型文件单选多选  multiple 属性的支持

资源有限 仅在华为手机上进行测试

private final static int  REQUEST_SELECT_FILE = 2;
private final static int FILECHOOSER_RESULTCODE = 1;
private ValueCallback uploadMessage; //多选文件回调
private ValueCallback mUploadMessage ; //单选文件回调

webView.setWebChromeClient(new WebChromeClient() {
	// For 3.0+ Devices (Start)
	protected void openFileChooser(ValueCallback uploadMsg, String acceptType){
		mUploadMessage = uploadMsg;
		Intent i = new Intent(Intent.ACTION_GET_CONTENT);
		i.addCategory(Intent.CATEGORY_OPENABLE);
		i.setType("image");
		startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
	}
	// For Lollipop 5.0+ Devices 目测应该5.0+才支持多选
	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public boolean onShowFileChooser(WebView mWebView,
									 ValueCallback filePathCallback,
									 WebChromeClient.FileChooserParams fileChooserParams){
		if (uploadMessage != null) {
			uploadMessage.onReceiveValue(null);
			uploadMessage = null;
		}
		uploadMessage = filePathCallback;
		Intent intent = fileChooserParams.createIntent();
		
		//此处增加参数  允许多选文件
		intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
		
		try{
			startActivityForResult(intent, REQUEST_SELECT_FILE);
		} catch (ActivityNotFoundException e){
			uploadMessage = null;
			Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	//For Android 4.1 only
	protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture){
		mUploadMessage = uploadMsg;
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image");
		startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
	}

	//for Android <3.0
	protected void openFileChooser(ValueCallback uploadMsg){
		mUploadMessage = uploadMsg;
		Intent i = new Intent(Intent.ACTION_GET_CONTENT);
		i.addCategory(Intent.CATEGORY_OPENABLE);
		i.setType("image");
		startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
	}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	super.onActivityResult(requestCode, resultCode, intent);
	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
		if (requestCode == REQUEST_SELECT_FILE){
			if (uploadMessage == null || intent == null) {
                //若没有选择文件就回退  执行清空  不进行这步操作无法再次选择文件
                uploadMessage.onReceiveValue(null);
                uploadMessage = null;
                return;
            }
			//由于用户可能单选或多选   单选getData  多选时getClipData()
			if(intent.getData() != null){
				uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
			}else if(intent.getClipData() != null){
				Uri[] uris = new Uri[intent.getClipData().getItemCount()];
				for(int i=0;i

完善了    选择文件时    未选择文件   操作回退按钮  的支持

2019年1月4日

完善了  用户操作未选择文件   回退后无法再次选择文件的问题  主要是在onActivityResult内进行onReceiveValue(null)

你可能感兴趣的:(android下webview实现H5 html标签 input file类型文件多选 multiple 属性的支持)