2017-12-18 121 views
0

我有一个button选择网站上的文件,当你按下你的计算机上的按钮它的作品,当你按下手机上的按钮不工作什么解决方案,该网站显示使用WebView选择文件不使用webview的手机上工作

 mywebsite = (WebView)findViewById(R.id.mywebsite); 
     pd_loading = (ProgressBar)findViewById(R.id.pd_loading); 
     mywebsite.getSettings().setLoadsImagesAutomatically(true); 
     mywebsite.getSettings().setJavaScriptEnabled(true); 


     mywebsite.setWebViewClient(new WebViewClient()); 
     mywebsite.loadUrl(Information.URL_Home); 
     mywebsite.setWebViewClient(new WebViewClient(){ 

      public void onPageFinished(WebView view ,String url) 
      { 
       pd_loading.setVisibility(View.GONE); 
       mywebsite.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { 
       Toasty.error(Home.this,"",Toast.LENGTH_SHORT,true).show(); 
      } 

      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { 

       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.addCategory(Intent.CATEGORY_OPENABLE); 
       i.setType("image/*"); 
       startActivityForResult(Intent.createChooser(i, "File Chooser"), 101); 
       mUploadMessage = uploadMsg; 
      } 

      // For Android > 4.1 - undocumented method 
      @SuppressWarnings("unused") 
      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { 
       openFileChooser(uploadMsg, ""); 

      } 

      // For Android > 5.0 
      @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
      public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { 

       startActivityForResult(fileChooserParams.createIntent(), 101); 
       return true; 
      } 

     }); 

onActivittyResult

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     switch (requestCode) { 
      case 101: 
       if (resultCode == RESULT_OK) { 

        Uri result = intent == null || resultCode != RESULT_OK ? null 
          : intent.getData(); 
        if (mUploadMessage != null) { 
         mUploadMessage.onReceiveValue(result); 
        } else if (afterLolipop != null) { 

         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
          afterLolipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); 
          afterLolipop = null; 
         } 
        } 
        mUploadMessage = null; 
       } 
     } 

    } 

这些代码不工作,我只是想选择我希望计算机的图片帮我感谢

+0

其中是按钮点击调用'openFileChooser'方法的JS代码? – Chithra

回答

0

当我遇到类似的问题时,我使用了this souce中的setWebChromeClient()方法。

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.webkit.JavascriptInterface; 
import android.webkit.ValueCallback; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 
import android.widget.Toast; 
import org.apache.http.util.EncodingUtils; 

public class BrowserScreen extends Activity { 

    private WebView webView; 
    private String url = "url"; 


    private ValueCallback<Uri> mUploadMessage; 
    private final static int FILECHOOSER_RESULTCODE = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.browser_activity); 
     initFields(); 
     setListeners(); 


    } 




    public void initFields() { 
     // TODO Auto-generated method stub 

     webView = (WebView) findViewById(R.id.webView1); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setBuiltInZoomControls(true); 
     webView.getSettings().setAllowFileAccess(true); 
    } 


    public void setListeners() { 
     // TODO Auto-generated method stub 

     webView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 

       webView.loadUrl("about:blank"); 

       view.clearHistory(); 
      } 


     }); 

     webView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 


      } 

      //The undocumented magic method override 
      //Eclipse will swear at you if you try to put @Override here 
      // For Android 3.0+ 
      public 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); 
      } 

      // For Android 3.0+ 
      public void openFileChooser(ValueCallback uploadMsg, String acceptType) { 
       mUploadMessage = uploadMsg; 
       Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
       i.addCategory(Intent.CATEGORY_OPENABLE); 
       i.setType("*/*"); 
       startActivityForResult(
         Intent.createChooser(i, "File Browser"), 
         FILECHOOSER_RESULTCODE); 
      } 

      //For Android 4.1 
      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("image/*"); 
       startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); 

      } 


     }); 



      webView.loadUrl(url); 


     final MyJavaScriptInterface myJavaScriptInterface 
       = new MyJavaScriptInterface(this); 
     webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction"); 
    } 


    @Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 

     if (webView.canGoBack() == true) { 
      webView.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 


    public class MyJavaScriptInterface { 
     Context mContext; 

     MyJavaScriptInterface(Context c) { 
      mContext = c; 
     } 

     @JavascriptInterface 
     public void showToast(String toast) { 
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
      // webView.loadUrl("javascript:document.getElementById(\"Button3\").innerHTML = \"bye\";"); 
     } 

     @JavascriptInterface 
     public void openAndroidDialog() { 
      AlertDialog.Builder myDialog 
        = new AlertDialog.Builder(BrowserScreen.this); 
      myDialog.setTitle("DANGER!"); 
      myDialog.setMessage("You can do what you want!"); 
      myDialog.setPositiveButton("ON", null); 
      myDialog.show(); 
     } 

    } 


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

不起作用,点击选择文件按钮未打开画廊请帮助我 – zain