2017-05-31 132 views
0

因此,我爲一個項目的朋友網站製作了一個WebView,該網站包含一個文件的上傳器,當您在常規移動/ PC瀏覽器上點擊它時它會打開filesbrowser/explorer來查找要上傳到網站的文件。文件資源管理器無法在webview上打開,在常規移動瀏覽器上打開

但是,在我所做的WebView的情況下,當我點擊文件上傳程序時,只需單擊即可,而不用手動啓動手機的文件瀏覽器,以便選擇我希望上傳的文件。

有什麼建議嗎?謝謝。

回答

1

@Simo您必須爲您的WebView設置chrome客戶端。請檢查下面的代碼

webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setAllowFileAccess(true); 
webView.getSettings().setAllowContentAccess(true); 
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
webView.setWebChromeClient(new WebChromeClient() 
    { 
     // For 3.0+ Devices (Start) 
     // onActivityResult attached before constructor 
     protected void openFileChooser(ValueCallback uploadMsg, String acceptType) 
     { 
      mUploadMessage = uploadMsg; 
      Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
      i.addCategory(Intent.CATEGORY_OPENABLE); 
      i.setType("image/*"); 
      startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); 
     } 


     // For Lollipop 5.0+ Devices 
     public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) 
     { 
      if (uploadMessage != null) { 
       uploadMessage.onReceiveValue(null); 
       uploadMessage = null; 
      } 

      uploadMessage = filePathCallback; 

      Intent intent = fileChooserParams.createIntent(); 
      try 
      { 
       startActivityForResult(intent, REQUEST_SELECT_FILE); 
      } catch (ActivityNotFoundException e) 
      { 
       uploadMessage = null; 
       Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); 
       return false; 
      } 
      return true; 
     } 

     //For Android 4.1 only 
     protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) 
     { 
      mUploadMessage = uploadMsg; 
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.addCategory(Intent.CATEGORY_OPENABLE); 
      intent.setType("image/*"); 
      startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); 
     } 

     protected void openFileChooser(ValueCallback<Uri> uploadMsg) 
     { 
      mUploadMessage = uploadMsg; 
      Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
      i.addCategory(Intent.CATEGORY_OPENABLE); 
      i.setType("image/*"); 
      startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); 
     } 
    }); 

而且覆蓋onActivityResult像下面

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    { 
     if (requestCode == REQUEST_SELECT_FILE) 
     { 
      if (uploadMessage == null) 
       return; 
      uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); 
      uploadMessage = null; 
     } 
    } 
    else if (requestCode == FILECHOOSER_RESULTCODE) 
    { 
     if (null == mUploadMessage) 
      return; 
     // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment 
     // Use RESULT_OK only if you're implementing WebView inside an Activity 
     Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); 
     mUploadMessage.onReceiveValue(result); 
     mUploadMessage = null; 
    } 
    else 
     Toast.makeText(getApplicationContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); 
} 

來源:WebView File Upload

+0

謝謝你,我會努力實現這個! –

相關問題