http://stackoverflow.com/questions/5907369/file-upload-in-webview
http://blog.csdn.net/longlingli/article/details/16946047
1 package com.example.cairh_sjkh_it; 2 3 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.app.AlertDialog.Builder; 7 import android.app.Notification.Action; 8 import android.content.Intent; 9 import android.content.res.Configuration; 10 import android.graphics.Bitmap; 11 import android.net.Uri; 12 import android.os.Build; 13 import android.os.Bundle; 14 import android.webkit.JsResult; 15 import android.webkit.ValueCallback; 16 import android.webkit.WebChromeClient; 17 import android.webkit.WebView; 18 import android.webkit.WebViewClient; 19 20 public class MainActivity extends Activity { 21 public ValueCallback<Uri> mUploadMessage; 22 public final static int FILECHOOSER_RESULTCODE = 1; 23 WebView webView; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.res); 28 webView=(WebView)findViewById(R.id.webview); 29 //js调试调试功能支持4.4版本以上的 30 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 31 WebView.setWebContentsDebuggingEnabled(true); 32 } 33 //js生效 34 webView.getSettings().setJavaScriptEnabled(true); 35 //让 h5页面中的 alert生效 36 webView.setWebChromeClient(new WebChromeClient(){ 37 //The undocumented magic method override 38 //Eclipse will swear at you if you try to put @Override here 39 // For Android 3.0+ 40 public void openFileChooser(ValueCallback<Uri> uploadMsg) { 41 42 mUploadMessage = uploadMsg; 43 Intent i = new Intent(Intent.ACTION_GET_CONTENT); 44 i.addCategory(Intent.CATEGORY_OPENABLE); 45 i.setType("image/*"); 46 MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE); 47 48 } 49 50 // For Android 3.0+ 51 public void openFileChooser( ValueCallback uploadMsg, String acceptType ) { 52 mUploadMessage = uploadMsg; 53 Intent i = new Intent(Intent.ACTION_GET_CONTENT); 54 i.addCategory(Intent.CATEGORY_OPENABLE); 55 i.setType("*/*"); 56 MainActivity.this.startActivityForResult( 57 Intent.createChooser(i, "File Browser"), 58 FILECHOOSER_RESULTCODE); 59 } 60 61 //For Android 4.1 62 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ 63 mUploadMessage = uploadMsg; 64 Intent i = new Intent(Intent.ACTION_GET_CONTENT); 65 i.addCategory(Intent.CATEGORY_OPENABLE); 66 i.setType("image/*"); 67 MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE ); 68 69 } 70 @Override 71 public boolean onJsAlert(WebView view,String url,String message,JsResult result) 72 { 73 return super.onJsAlert(view,url,message,result); 74 } }); 75 webView.setWebViewClient(new WebViewClient(){ 76 @Override 77 public boolean shouldOverrideUrlLoading(WebView view,String url){ 78 // TODO Auto-generated method stub 79 // 使用自己的WebView组件来响应Url加载事件,而不是使用默认浏览器器加载页面 80 view.loadUrl(url); 81 return super.shouldOverrideUrlLoading(view, url); 82 } 83 }); 84 webView.getSettings().setDomStorageEnabled(true); 85 webView.getSettings().setDatabaseEnabled(true); 86 87 webView.loadUrl("http://192.168.226.153:8080/mobile/index.htm"); 88 89 } 90 @Override 91 protected void onActivityResult(int requestCode, int resultCode, 92 Intent intent) { 93 if(requestCode==FILECHOOSER_RESULTCODE) 94 { 95 if (null == mUploadMessage) return; 96 Uri result = intent == null || resultCode != RESULT_OK ? null 97 : intent.getData(); 98 mUploadMessage.onReceiveValue(result); 99 mUploadMessage = null; 100 } 101 } 102 103 public class myWebClient extends WebViewClient 104 { 105 @Override 106 public void onPageStarted(WebView view, String url, Bitmap favicon) { 107 // TODO Auto-generated method stub 108 super.onPageStarted(view, url, favicon); 109 } 110 111 @Override 112 public boolean shouldOverrideUrlLoading(WebView view, String url) { 113 // TODO Auto-generated method stub 114 115 view.loadUrl(url); 116 return true; 117 118 } 119 120 @Override 121 public void onPageFinished(WebView view, String url) { 122 // TODO Auto-generated method stub 123 super.onPageFinished(view, url); 124 125 } 126 } 127 128 //flipscreen not loading again 129 @Override 130 public void onConfigurationChanged(Configuration newConfig){ 131 super.onConfigurationChanged(newConfig); 132 } 133 134 // To handle "Back" key press event for WebView to go back to previous screen. 135 /*@Override 136 public boolean onKeyDown(int keyCode, KeyEvent event) 137 { 138 if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { 139 web.goBack(); 140 return true; 141 } 142 return super.onKeyDown(keyCode, event); 143 }*/ 144 }
时间: 2024-10-16 05:33:08