本文主要是介绍Android WebView加载 不支持 ElementUi el-upload文件上传无法正常打开文件列表 解决方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了H5并嵌入webview中。发现点击H5中ElementUi el-upload 标签 不能打开android资源管理器。
通过网络搜索发现是因为 android webview 由于考虑安全原因屏蔽了 input type="file" 这个功能 。
经过不懈的努力,以及google 翻译的帮助 在 stackoverflow 中找到了解决的方法。
具体可以理解为 重写webview 的WebChromeClient :
重写webview 的WebChromeClient:
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 2;
webView.setWebChromeClient(new WebChromeClient(){// For 3.0+ Devices (Start)// onActivityResult attached before constructorprotected 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@TargetApi(Build.VERSION_CODES.LOLLIPOP)@Overridepublic boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {if (uploadMessage != null) {uploadMessage.onReceiveValue(null);uploadMessage = null;}uploadMessage = filePathCallback;Intent intent = fileChooserParams.createIntent();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 onlyprotected void openFileChooser(ValueCallback<Uri> 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);}protected void openFileChooser(ValueCallback<Uri> 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 public void onActivityResult(int requestCode, int resultCode, Intent intent) {if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){if (requestCode == REQUEST_SELECT_FILE){if (uploadMessage == null)return;uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));uploadMessage = null;}}else if (requestCode == FILECHOOSER_RESULTCODE){if (null == mUploadMessage)return;// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment// Use RESULT_OK only if you're implementing WebView inside an ActivityUri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();mUploadMessage.onReceiveValue(result);mUploadMessage = null;}elseToast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); }
这篇关于Android WebView加载 不支持 ElementUi el-upload文件上传无法正常打开文件列表 解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!