浅谈关于Android WebView上传文件的解决方案
我们在开发需求的时候,难免会接入一下第三方的H5页面,有些H5页面是具有上传照片的功能,Android中的WebView是不能直接打开文件选择弹框的
接下来我讲简单提供一下解决方案,先说一下思路
1.接收WebView打开文件选择器的通知
2.收到通知后,打开文件选择器等待用户选择需要上传的文件
3.在onActivityResult中得到用户选择的文件的Uri
4.然后把Uri传递给Html5
这样就完成了一次H5选择文件的过程,下面我把代码贴出来自习看一下
首先,WebView必须要支持JS交互,所以要打开JS交互
mWebView.getSettings().setJavaScriptEnabled(true);
当H5在调用上传文件的Api的时候,WebView会回调openFileChooser和onShowFileChooser方法来通知我们,我们这个时候要做的就是重写这个方法
需要注意的是这个方法在不同的Api上会回调不同行参方法
mWebView.setWebChromeClient(newWebChromeClient(){ @Override publicvoidonProgressChanged(WebViewview,intnewProgress){ if(newProgress==100){ mBar.setVisibility(View.GONE); }else{ mBar.setVisibility(View.VISIBLE); mBar.setProgress(newProgress); } super.onProgressChanged(view,newProgress); } //ForAndroidAPI<11(3.0OS) publicvoidopenFileChooser(ValueCallbackvalueCallback){ uploadMessage=valueCallback; openImageChooserActivity(); } //ForAndroidAPI>=11(3.0OS) publicvoidopenFileChooser(ValueCallback valueCallback,StringacceptType,Stringcapture){ uploadMessage=valueCallback; openImageChooserActivity(); } //ForAndroidAPI>=21(5.0OS) @Override publicbooleanonShowFileChooser(WebViewwebView,ValueCallback filePathCallback,WebChromeClient.FileChooserParamsfileChooserParams){ uploadMessageAboveL=filePathCallback; openImageChooserActivity(); returntrue; } });
我们在openFileChooser方法中先保存了一下ValueCallback的回调对象,这个对象最后用来通知H5文件地址,我们之后在调用openFileChooser方法来打开文件选择器
privatevoidopenImageChooserActivity(){ Intenti=newIntent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i,"ImageChooser"),FILE_CHOOSER_RESULT_CODE); }
当用户选择完文件后,会调用onActivityResult方法,我们重写并等待回调
@Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ super.onActivityResult(requestCode,resultCode,data); if(requestCode==FILE_CHOOSER_RESULT_CODE){ if(null==uploadMessage&&null==uploadMessageAboveL)return; Uriresult=data==null||resultCode!=RESULT_OK?null:data.getData(); if(uploadMessageAboveL!=null){ onActivityResultAboveL(requestCode,resultCode,data); }elseif(uploadMessage!=null){ uploadMessage.onReceiveValue(result); uploadMessage=null; } } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) privatevoidonActivityResultAboveL(intrequestCode,intresultCode,Intentintent){ if(requestCode!=FILE_CHOOSER_RESULT_CODE||uploadMessageAboveL==null) return; Uri[]results=null; if(resultCode==Activity.RESULT_OK){ if(intent!=null){ StringdataString=intent.getDataString(); ClipDataclipData=intent.getClipData(); if(clipData!=null){ results=newUri[clipData.getItemCount()]; for(inti=0;ionActivityResult就是用来通知H5用户选择的文件地址,在这个方法里,用我们之前保存的ValueCallback对象,调用onReceiveValue方法,H5就可以收到我们传递给它的地址信息了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。