今天就跟大家聊聊有关Android中怎么利用WebView上传文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
接收WebView打开文件选择器的通知
收到通知后,打开文件选择器等待用户选择需要上传的文件
在onActivityResult中得到用户选择的文件的Uri
然后把Uri传递给Html5
这样就完成了一次H5选择文件的过程,下面我把代码贴出来自习看一下
首先,WebView必须要支持JS交互,所以要打开JS交互
mWebView.getSettings().setJavaScriptEnabled(true);
当H5在调用上传文件的Api的时候,WebView会回调 openFileChooser和onShowFileChooser 方法来通知我们,我们这个时候要做的就是重写这个方法
需要注意的是这个方法在不同的Api上会回调不同行参方法
mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { mBar.setVisibility(View.GONE); } else { mBar.setVisibility(View.VISIBLE); mBar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } //For Android API < 11 (3.0 OS) public void openFileChooser(ValueCallback<Uri> valueCallback) { uploadMessage = valueCallback; openImageChooserActivity(); } //For Android API >= 11 (3.0 OS) public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) { uploadMessage = valueCallback; openImageChooserActivity(); } //For Android API >= 21 (5.0 OS) @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { uploadMessageAboveL = filePathCallback; openImageChooserActivity(); return true; } });
我们在openFileChooser方法中先保存了一下ValueCallback的回调对象,这个对象最后用来通知H5文件地址,我们之后在调用openFileChooser方法来打开文件选择器
private void openImageChooserActivity() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE); }
当用户选择完文件后,会调用onActivityResult方法,我们重写并等待回调
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILE_CHOOSER_RESULT_CODE) { if (null == uploadMessage && null == uploadMessageAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (uploadMessageAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (uploadMessage != null) { uploadMessage.onReceiveValue(result); uploadMessage = null; } } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) { if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null) return; Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (intent != null) { String dataString = intent.getDataString(); ClipData clipData = intent.getClipData(); if (clipData != null) { results = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); results[i] = item.getUri(); } } if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; } } uploadMessageAboveL.onReceiveValue(results); uploadMessageAboveL = null; }
看完上述内容,你们对Android中怎么利用WebView上传文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。