Andriod webview实现文件上传

  1. 在国外网站上看到的挺不错的 关于WebView的   <a target="_blank" href="http://stackoverflow.com/questions/5907369/file-upload-in-webview">http://stackoverflow.com/questions/5907369/file-upload-in-webview</a>  
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MyWb extends Activity {  
  2. /** Called when the activity is first created. */  
  3.   
  4. WebView web;  
  5. ProgressBar progressBar;  
  6.   
  7. private ValueCallback<Uri> mUploadMessage;    
  8.  private final static int FILECHOOSER_RESULTCODE=1;    
  9.   
  10.  @Override    
  11.  protected void onActivityResult(int requestCode, int resultCode,    
  12.                                     Intent intent) {    
  13.   if(requestCode==FILECHOOSER_RESULTCODE)    
  14.   {    
  15.    if (null == mUploadMessage) return;    
  16.             Uri result = intent == null || resultCode != RESULT_OK ? null    
  17.                     : intent.getData();    
  18.             mUploadMessage.onReceiveValue(result);    
  19.             mUploadMessage = null;    
  20.   }  
  21.   }    
  22.   
  23. @Override  
  24. public void onCreate(Bundle savedInstanceState) {  
  25.     super.onCreate(savedInstanceState);  
  26.     setContentView(R.layout.main);  
  27.   
  28.     web = (WebView) findViewById(R.id.webview01);  
  29.     progressBar = (ProgressBar) findViewById(R.id.progressBar1);  
  30.   
  31.     web = new WebView(this);    
  32.     web.getSettings().setJavaScriptEnabled(true);  
  33.     web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");  
  34.     web.setWebViewClient(new myWebClient());  
  35.     web.setWebChromeClient(new WebChromeClient()    
  36.     {    
  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.             MyWb.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.            MyWb.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.                MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );  
  68.   
  69.            }  
  70.   
  71.     });    
  72.   
  73.   
  74.     setContentView(web);    
  75.   
  76.   
  77. }  
  78.   
  79. public class myWebClient extends WebViewClient  
  80. {  
  81.     @Override  
  82.     public void onPageStarted(WebView view, String url, Bitmap favicon) {  
  83.         // TODO Auto-generated method stub  
  84.         super.onPageStarted(view, url, favicon);  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  89.         // TODO Auto-generated method stub  
  90.   
  91.         view.loadUrl(url);  
  92.         return true;  
  93.   
  94.     }  
  95.   
  96.     @Override  
  97.     public void onPageFinished(WebView view, String url) {  
  98.         // TODO Auto-generated method stub  
  99.         super.onPageFinished(view, url);  
  100.   
  101.         progressBar.setVisibility(View.GONE);  
  102.     }  
  103. }  
  104.   
  105. //flipscreen not loading again  
  106. @Override  
  107. public void onConfigurationChanged(Configuration newConfig){          
  108.     super.onConfigurationChanged(newConfig);  
  109. }  
  110.   
  111. // To handle "Back" key press event for WebView to go back to previous screen.  
  112. /*@Override  
  113. public boolean onKeyDown(int keyCode, KeyEvent event)   
  114. {  
  115.     if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {  
  116.         web.goBack();  
  117.         return true;  
  118.     }  
  119.     return super.onKeyDown(keyCode, event);  
  120. }*/  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值