2016-03-25 53 views
0

我創建了一個WebView,它顯示帶有輸入類型文件的網頁。但選擇文件後,我的webview不會對任何內容做出反應,只能用於webView外的對象(刷新按鈕)。我該如何解決它?WebView在文件選擇器後沒有反應

的問題發生在安卓4.1.2

我的代碼:

package com.gymcourses.qiteq.pai16; 

import android.Manifest; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.Uri; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.ActionBar; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.webkit.GeolocationPermissions; 
import android.webkit.ValueCallback; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 

public class MainActivity extends AppCompatActivity { 

    private final int SPLASH_DISPLAY_LENGTH = 1000; 
    private final static int FILECHOOSER_RESULTCODE=1; 
    ProgressBar progressBar; 
    public WebView webView; 
    private ValueCallback<Uri> mUploadMessage; 
    ImageView refreshBtn; 
    private ValueCallback<Uri[]> mFilePathCallback; 
    private Uri mCapturedImageURI = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (isNetworkConnected()) Log.v("INTERNET: ", "true"); 
     else Log.v("INTERNET: ", "false;"); 

     if (isNetworkConnected()) { 
      refreshBtn = (ImageView) findViewById(R.id.refreshBtn); 

      progressBar = (ProgressBar) findViewById(R.id.progressBar); 
      refreshBtn.setVisibility(View.INVISIBLE); 
      ActionBar actionBar = getSupportActionBar(); 
      actionBar.hide(); 

      webView = (WebView) findViewById(R.id.webView); 
      webView.setVisibility(View.INVISIBLE); 
      webView.loadUrl("http://something.com"); 
      webView.getSettings().setJavaScriptEnabled(true); 
      webView.getSettings().setGeolocationEnabled(true); 
      webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
      webView.getSettings().setBuiltInZoomControls(true); 

      webView.getSettings().setSaveFormData(false); 
      webView.getSettings().setSavePassword(false); 
      webView.getSettings().setAppCacheEnabled(true); 
      webView.getSettings().setDatabaseEnabled(true); 
      webView.getSettings().setDomStorageEnabled(true); 
      webView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath()); 



      WebChromeClient webChromeClient = new WebChromeClient(){ 

       @Override 
       public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { 
        // callback.invoke(String origin, boolean allow, boolean remember); 
        callback.invoke(origin, true, false); 
       } 

       public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){ 
        mUploadMessage = uploadMsg; 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE); 
        i.setType("*/*"); 
        MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE); 

       } 

       public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){ 
        Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
        i.addCategory(Intent.CATEGORY_OPENABLE); 
        i.setType("*/*"); 
        MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE); 
        return false; 
       } 

      }; 

      WebViewClient webViewClient = new WebViewClient(){ 
       @Override 
       public boolean shouldOverrideUrlLoading(WebView view, String url){ 
        view.loadUrl(url); 
        return true; 
       } 

       public void onPageFinished(WebView view, String url){ 
        progressBar.setVisibility(View.GONE); 
        webView.setVisibility(View.VISIBLE); 
        refreshBtn.setVisibility(View.VISIBLE); 
       } 
      }; 

      webView.setWebChromeClient(webChromeClient); 
      webView.setWebViewClient(webViewClient); 

      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0); 
     } 
     else{ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(getString(R.string.alertDialogMessage)) 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          System.exit(0); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 

    } 

    public void refresh(View view){ 
     webView.reload(); 
     refreshBtn.setVisibility(View.INVISIBLE); 
     webView.setVisibility(View.INVISIBLE); 
     progressBar.setVisibility(View.VISIBLE); 
    } 

    public boolean isNetworkConnected(){ 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     return cm.getActiveNetworkInfo() != null; 
    } 
} 

回答

0

試試這個例子使用新版本的API來調整它,檢查FileChooser的新實現。

public class WebViewActivity extends ActionBarActivity { 
     private final static int FILECHOOSER_RESULTCODE = 1; 
     private final static String url = "https://www.cs.tut.fi/~jkorpela/forms/file.html"; 
     public static WebViewActivity _activity; 
     ProgressDialog progressBar; 
     ProgressBar progressBar1; 
     AlertDialog alertDialog; 
     boolean loadingFinished = true; 
     boolean redirect = false; 
     Menu menu; 
     WebView wv; 
     private ValueCallback<Uri> mUploadMessage; 
     private String TAG = "WebViewActivity"; 

     public static boolean checkInternetConnection(Activity _activity) { 
      ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE); 
      return conMgr.getActiveNetworkInfo() != null 
        && conMgr.getActiveNetworkInfo().isAvailable() 
        && conMgr.getActiveNetworkInfo().isConnected(); 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      _activity = this; 
      wv = new WebView(this); 
      WebSettings settings = wv.getSettings(); 
      settings.setJavaScriptEnabled(true); 
      settings.setSupportZoom(true); 
      settings.setBuiltInZoomControls(true); 
      settings.setSaveFormData(true); 
      settings.setSavePassword(true); // Not needed for API level 18 or greater (deprecated) 
      CookieManager cookieManager = CookieManager.getInstance(); 
      cookieManager.setAcceptCookie(true); 
      if (cookieManager.hasCookies()) { 
       Log.d(TAG, "has cookies"); 
      } else { 
       Log.d(TAG, "has not cookies"); 
      } 

      CookieSyncManager.createInstance(this); 
      CookieSyncManager.getInstance().startSync(); 


      wv.setDownloadListener(new DownloadListener() { 
       public void onDownloadStart(String url, String userAgent, 
              String contentDisposition, String mimetype, 
              long contentLength) { 
        Intent i = new Intent(Intent.ACTION_VIEW); 
        i.setData(Uri.parse(url)); 
        startActivity(i); 
       } 
      }); 
      wv.setWebChromeClient(new WebChromeClient() { 
       //The undocumented magic method override 
       //Eclipse will swear at you if you try to put @Override here 
       public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
        WebViewActivity.this.showAttachmentDialog(uploadMsg); 
       } 

       // For Android > 3.x 
       public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { 
        WebViewActivity.this.showAttachmentDialog(uploadMsg); 
       } 

       // For Android > 4.1 
       public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { 
        WebViewActivity.this.showAttachmentDialog(uploadMsg); 
       } 
      }); 

      this.setContentView(wv); 

      if (checkInternetConnection(_activity) == true) { 
       if (savedInstanceState == null) { 

        wv.loadUrl(url); 
       } else 

        wv.loadUrl(url); 
       alertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK).create(); 
       alertDialog.setCancelable(true); 
       progressBar = ProgressDialog.show(WebViewActivity.this, getResources().getString(R.string.patientez), 
    getResources().getString(R.string.chargement)); 
       progressBar.setCancelable(true); 
       wv.setWebViewClient(new WebViewClient() { 
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) { 
         if (!loadingFinished) { 
          redirect = true; 
         } 
         loadingFinished = false; 
         wv.loadUrl(urlNewString); 
         return true; 
        } 

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
         handler.proceed(); 
        } 

        @Override 
        public void onPageFinished(WebView view, String url) { 
         Log.d(TAG, "onPageFinished"); 
         //CookieSyncManager.getInstance().sync(); 
         if (!redirect) { 
          loadingFinished = true; 
         } 
         if (loadingFinished && !redirect) { 
          //HIDE LOADING IT HAS FINISHED 
          if (progressBar != null && progressBar.isShowing()) { 
           progressBar.hide(); 
          } 
         } else { 
          redirect = false; 
         } 
        } 

        @Override 
        public void onPageStarted(WebView view, String url, Bitmap favicon) { 
         super.onPageStarted(view, url, favicon); 
         loadingFinished = false; 
         progressBar.show(); 
        } 
       }); 
      } else { 
       AlertDialog.Builder builder = new AlertDialog.Builder(_activity); 
       builder.setTitle(R.string.configDoc); 
       builder.setMessage(R.string.pro_refresh) 
         .setCancelable(false) 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           Intent intent = new Intent(getApplicationContext(), ProActivity.class); 
           //intent.addCategory(Intent.CATEGORY_HOME); 
           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(intent); 
           finish(); 
          } 
         }); 

       AlertDialog alert = builder.create(); 
       alert.show(); 
      } 

     } 

     // to know if upload is finished 


     private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) { 
      this.mUploadMessage = uploadMsg; 

      Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
      i.addCategory(Intent.CATEGORY_OPENABLE); 
      i.setType("*/*"); 

      this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE); 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
      if (requestCode == FILECHOOSER_RESULTCODE) { 
       if (null == this.mUploadMessage) { 
        return; 
       } 
       Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); 
       this.mUploadMessage.onReceiveValue(result); 
       this.mUploadMessage = null; 
      } 
     } 


     @Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) { 
       //if Back key pressed and webview can navigate to previous page 
       wv.goBack(); 
       // go back to previous page 
       return true; 
      } else { 
       finish(); 
       // finish the activity 
      } 
      return super.onKeyDown(keyCode, event); 
     } 

     @Override 
     public void onBackPressed() { 
      // Do Some thing Here 
      // this.finish(); 
      super.onBackPressed(); 

     } 


     @Override 
     protected void onDestroy() { 
      super.onDestroy(); //To change body of overridden methods use File | Settings | File Templates. 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); //To change body of overridden methods use File | Settings | File Templates. 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); //To change body of overridden methods use File | Settings | File Templates. 
     } 

     @Override 
     protected void onStop() { 
      super.onStop(); //To change body of overridden methods use File | Settings | File Templates. 
     } 

     @Override 
     public void onConfigurationChanged(Configuration newConfig) { 
      super.onConfigurationChanged(newConfig); 
     } } 
+0

它適用於我,但它造成了另一個問題。在FileChooser中選擇一些內容後,它不會上傳文件,只是出去。 – Qiteq

0

試圖實現您的onActivityResult。重要的是onReceiveValue()方法的調用,您可以使用githhub https://github.com/henrychuangtw/Kitkat-WebView中的示例進行測試。這是一個ActivityResult的示例: private ValueCallback mFIlePathCallback; private ValueCallback mFIlePathCallbackLollipop;

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     Log.d(TAG, "onActivityResult"); 

     if ((requestCode == mFilechooserResultcode) && (resultCode == RESULT_OK)) { 
      if ((mFIlePathCallback == null) && (mFIlePathCallbackLollipop == null)) { 
       return; 
      } 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       //lollipop + 
       String dataString = intent.getDataString(); 

       if (dataString != null) { 
        Uri[] result = new Uri[]{Uri.parse(dataString)}; 
        mFIlePathCallbackLollipop.onReceiveValue(result); 
        mFIlePathCallbackLollipop = null; 
       } 
      } else { 
       //kitkat - 
       Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); 
       mFIlePathCallback.onReceiveValue(result); 
       mFIlePathCallback = null; 
      } 
     } 
    } 
+0

什麼是mFilechooserResultcode? – Qiteq

+0

這是您用來保存ActivityFor結果的代碼。代碼爲MainActivity.FILECHOOSER_RESULTCODE。 – user2043602

+0

@Qiteq如果你想了解更多關於StartActivityForResult的信息http://www.javatpoint.com/android-startactivityforresult-example – user2043602