Android應用開發中webview上傳文件的幾種思路

1. 常規方法,重寫WebChromeClient 的 openFileChooser 方法java

private class MyWebChromeClient extends WebChromeClient {        
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {  
               if (mUploadMessage != null) return;
               mUploadMessage = uploadMsg;   
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);
               i.addCategory(Intent.CATEGORY_OPENABLE);
               i.setType("*/*");
               
                   startActivityForResult( Intent.createChooser( i, "File Chooser" ), 1 );
         }
            // For Android < 3.0
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
               openFileChooser( uploadMsg, "" );
        }
     // For Android  > 4.1.1  
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) 
        {              
            openFileChooser(uploadMsg, acceptType);      
        }
       
             
    }

這個是你們經常使用的,可是這個openFileChooser不是公開的方法,android4.4 不支持,腫麼辦?判斷版本,若是是4.4就調用系統瀏覽器是個變通的方法,到時能夠用,可是客戶不承認,只有本身用app本身實現上傳了。android

2. 本身實現上傳web

private String post(String pathToOurFile) throws ClientProtocolException, IOException, JSONException {
          HttpClient httpclient = new DefaultHttpClient();
          //設置通訊協議版本
          httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
           
          HttpPost httppost = new HttpPost(uploadUrl);
          File file = new File(pathToOurFile);
       
          MultipartEntity mpEntity = new MultipartEntity(); //文件傳輸
          ContentBody cbFile = new FileBody(file);
          mpEntity.addPart("userfile", cbFile); // <input type="file" name="userfile" />  對應的
       
       
          httppost.setEntity(mpEntity);
           
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity resEntity = response.getEntity();
       
          String json="";
          String path="";
          if (resEntity != null) {
            json=EntityUtils.toString(resEntity,"utf-8");
            JSONObject p=null;
            try{
                p=new JSONObject(json);
                path=(String) p.get("path");
            }catch(Exception e){
                e.printStackTrace();
            }
          }
          if (resEntity != null) {
            resEntity.consumeContent();
          }
       
          httpclient.getConnectionManager().shutdown();
          return path;
      }

用到了 httpmine包,網上有下載。json

如何調用呢?瀏覽器

browser.addJavascriptInterface(this, "android"); browser是webview對象。app

在頁面<a href="javascrijpt:window.android.uploadFile()">上傳文件</a>ide

uploadFile() 是被調用的Activity的一個方法,主要就是打開選擇對話框,流程和第一種方法同樣。只是返回文件路徑後本身上傳。post

新版android系統到是能夠了,又出現了問題:在android2.3等版本有個bug,從js調用app的方法會崩潰,不兼容,咱們只能換一種方法調用這個uploadFilethis

另外編譯的時候最好選擇2.2版本,不要選擇2.3url

3. 過濾url, 重寫 WebViewClient的 shouldOverrideUrlLoading方法

private class MyWebViewClient extends WebViewClient{
          private Context mContext;
          public MyWebViewClient(Context context){
           super();
           mContext = context;
          }
          
          public boolean shouldOverrideUrlLoading(WebView view, String url)
          {
              if (url.equalsIgnoreCase("app:upload")) {
                  MainActivity.this.uploadFile();
                  return true;
              }
              view.loadUrl(url);
              return true;
          }
         }  

頁面中 <a href="app:upload">上傳文件</a>

Android 的兼容性問題真讓人頭疼啊 --

相關文章
相關標籤/搜索