Android中的webview支持页面中的文件上传实例代码
Androidwebview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有
但是点击按钮之后没有反应...
那么如何能够让android的webview能够响应,这个浏览按钮呢?在网上查了很多资料,很多相同的,但都漏掉了一个地方,导致无法读取到文件的完整地址(“c:\upfile\233232.jpg”),整理最终代码入下:
我们需要为webview设置WebChromeClient,在WebChromeClient的实现类中覆盖文件选择的方法:
packagecom.example.webviewupfile; importjava.io.File; importjava.io.IOException; importandroid.app.Activity; importandroid.content.ContentResolver; importandroid.content.Intent; importandroid.content.res.Configuration; importandroid.database.Cursor; importandroid.graphics.Bitmap; importandroid.net.Uri; importandroid.os.Bundle; importandroid.provider.MediaStore; importandroid.util.Log; importandroid.view.View; importandroid.webkit.ValueCallback; importandroid.webkit.WebChromeClient; importandroid.webkit.WebView; importandroid.webkit.WebViewClient; importandroid.widget.ProgressBar; publicclassMainActivityextendsActivity{ privateValueCallbackmUploadMessage; privatefinalstaticintFILECHOOSER_RESULTCODE=1; privateWebViewweb; privateProgressBarprogressBar; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); web=(WebView)findViewById(R.id.webView1); progressBar=(ProgressBar)findViewById(R.id.progressBar1); web=newWebView(this); web.getSettings().setJavaScriptEnabled(true); web.loadUrl("http://ueditor.baidu.com/website/onlinedemo.html"); web.setWebViewClient(newmyWebClient()); web.setWebChromeClient(newWebChromeClient(){ //Theundocumentedmagicmethodoverride //Eclipsewillswearatyouifyoutrytoput@Overridehere //ForAndroid3.0+ publicvoidopenFileChooser(ValueCallback uploadMsg){ mUploadMessage=uploadMsg; Intenti=newIntent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i,"FileChooser"), FILECHOOSER_RESULTCODE); } //ForAndroid3.0+ publicvoidopenFileChooser(ValueCallbackuploadMsg, StringacceptType){ mUploadMessage=uploadMsg; Intenti=newIntent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i,"FileBrowser"), FILECHOOSER_RESULTCODE); } //ForAndroid4.1 publicvoidopenFileChooser(ValueCallback uploadMsg, StringacceptType,Stringcapture){ mUploadMessage=uploadMsg; Intenti=newIntent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i,"FileChooser"), MainActivity.FILECHOOSER_RESULTCODE); } }); setContentView(web); } @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; Bitmapbm=null; //外界的程序访问ContentProvider所提供数据可以通过ContentResolver接口 ContentResolverresolver=getContentResolver(); try{ UrioriginalUri=intent.getData();//获得图片的uri bm=MediaStore.Images.Media.getBitmap(resolver,originalUri); //这里开始的第二部分,获取图片的路径: String[]proj={MediaStore.Images.Media.DATA}; //好像是android多媒体数据库的封装接口,具体的看Android文档 Cursorcursor=managedQuery(originalUri,proj,null,null, null); //按我个人理解这个是获得用户选择的图片的索引值 intcolumn_index=cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); //将光标移至开头,这个很重要,不小心很容易引起越界 cursor.moveToFirst(); //最后根据索引值获取图片路径 Stringpath=cursor.getString(column_index); Uriuri=Uri.fromFile(newFile(path)); mUploadMessage.onReceiveValue(uri); }catch(IOExceptione){ Log.e("TAG-->Error",e.toString()); } } } publicclassmyWebClientextendsWebViewClient{ @Override publicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon){ //TODOAuto-generatedmethodstub super.onPageStarted(view,url,favicon); } @Override publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){ //TODOAuto-generatedmethodstub view.loadUrl(url); returntrue; } @Override publicvoidonPageFinished(WebViewview,Stringurl){ //TODOAuto-generatedmethodstub super.onPageFinished(view,url); progressBar.setVisibility(View.GONE); } } //flipscreennotloadingagain @Override publicvoidonConfigurationChanged(ConfigurationnewConfig){ super.onConfigurationChanged(newConfig); } //Tohandle"Back"keypresseventforWebViewtogobacktoprevious //screen. /* *@OverridepublicbooleanonKeyDown(intkeyCode,KeyEventevent){if *((keyCode==KeyEvent.KEYCODE_BACK)&&web.canGoBack()){web.goBack(); *returntrue;}returnsuper.onKeyDown(keyCode,event);} */ }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。