Android webview打开本地图片上传实现代码
Webview打开本地图片选择器十分之麻烦,其在安卓系统3x4x5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。
直接上代码
packagecom.testandroid.webview; importandroid.content.Intent; importandroid.net.Uri; importandroid.os.Bundle; importandroid.support.v7.app.AlertDialog; importandroid.support.v7.app.AppCompatActivity; importandroid.view.KeyEvent; importandroid.view.View; importandroid.webkit.JsResult; importandroid.webkit.ValueCallback; importandroid.webkit.WebBackForwardList; importandroid.webkit.WebChromeClient; importandroid.webkit.WebView; importandroid.webkit.WebViewClient; importandroid.widget.Button; importcom.testandroid.R; publicclassWebViewActivityextendsAppCompatActivity{ privatefinalStringTAG=WebViewActivity.class.getSimpleName(); privateButtonbutton; privateWebViewwebView; privateStringrecgPic="http://m.shitu.chinaso.com/mx/index.html"; publicfinalstaticintFILECHOOSER_RESULTCODE=1; publicfinalstaticintFILECHOOSER_RESULTCODE_FOR_ANDROID_5=2; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); button=(Button)findViewById(R.id.button); button.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ } }); initTestWebView(); } privatevoidinitTestWebView(){ webView=(WebView)findViewById(R.id.tempWebView); WiewSettingssettings=webView.getSettings(); settings.setJavaScriptEnabled(true); webView.setWebChromeClient(newWebChromeClient(){ @Override publicbooleanonJsAlert(WebViewview,Stringurl,Stringmessage,JsResultresult){ AlertDialog.Builderbuilder=newAlertDialog.Builder(view.getContext()); builder.setTitle("xxx提示").setMessage(message).setPositiveButton("确定",null); builder.setCancelable(false); builder.setIcon(R.mipmap.ic_launcher); AlertDialogdialog=builder.create(); dialog.show(); result.confirm(); returntrue; } //扩展浏览器上传文件 //3.0++版本 publicvoidopenFileChooser(ValueCallback<Uri>uploadMsg,StringacceptType){ openFileChooserImpl(uploadMsg); } //3.0--版本 publicvoidopenFileChooser(ValueCallback<Uri>uploadMsg){ openFileChooserImpl(uploadMsg); } publicvoidopenFileChooser(ValueCallback<Uri>uploadMsg,StringacceptType,Stringcapture){ openFileChooserImpl(uploadMsg); } @Override publicbooleanonShowFileChooser(WebViewwebView,ValueCallback<Uri[]>filePathCallback,FileChooserParamsfileChooserParams){ onenFileChooseImpleForAndroid(filePathCallback); returntrue; } }); webView.setWebViewClient(newWebViewClient(){ @Override publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){ view.loadUrl(url); returntrue; } }); webView.loadUrl(recgPic); } publicValueCallback<Uri>mUploadMessage; privatevoidopenFileChooserImpl(ValueCallback<Uri>uploadMsg){ mUploadMessage=uploadMsg; Intenti=newIntent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i,"FileChooser"),FILECHOOSER_RESULTCODE); } publicValueCallback<Uri[]>mUploadMessageForAndroid5; privatevoidonenFileChooseImpleForAndroid(ValueCallback<Uri[]>filePathCallback){ mUploadMessageForAndroid5=filePathCallback; IntentcontentSelectionIntent=newIntent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); IntentchooserIntent=newIntent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT,contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE,"ImageChooser"); startActivityForResult(chooserIntent,FILECHOOSER_RESULTCODE_FOR_ANDROID_5); } @Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentintent){ if(requestCode==FILECHOOSER_RESULTCODE){ if(null==mUploadMessage) return; Uriresult=intent==null||resultCode!=RESULT_OK?null:intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage=null; }elseif(requestCode==FILECHOOSER_RESULTCODE_FOR_ANDROID_5){ if(null==mUploadMessageForAndroid5) return; Uriresult=(intent==null||resultCode!=RESULT_OK)?null:intent.getData(); if(result!=null){ mUploadMessageForAndroid5.onReceiveValue(newUri[]{result}); }else{ mUploadMessageForAndroid5.onReceiveValue(newUri[]{}); } mUploadMessageForAndroid5=null; } } @Override publicbooleanonKeyDown(intkeyCode,KeyEventevent){ if(webView.canGoBack()&&event.getKeyCode()==KeyEvent.KEYCODE_BACK){ //获取历史列表 WebBackForwardListmWebBackForwardList=webView .copyBackForwardList(); //判断当前历史列表是否最顶端,其实canGoBack已经判断过 if(mWebBackForwardList.getCurrentIndex()>0){ webView.goBack(); returntrue; } } returnsuper.onKeyDown(keyCode,event); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。