2017-12-18 2 views
0

Ich habe eine button wählen Sie Dateien in der Website, wenn Sie die Taste auf Ihrem Computer drücken funktioniert es und wenn Sie die Taste auf dem Telefon drücken funktioniert nicht, welche Lösung, die Website zeigt mit WebViewwählen Sie Dateien funktionieren nicht am Telefon mit 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; 
       } 
     } 

    } 

diese Codes nicht funktionieren ich will nur ein Bild von einem Computer wählen ich hoffe, mir helfen, dank

+0

Wo ist der JS-Code zum Aufrufen der Methode 'openFileChooser' beim Klicken auf die Schaltfläche? – Chithra

Antwort

0

Als ich eine ähnliche Art von Problem konfrontiert, habe ich setWebChromeClient() Methode von this souce gefunden.

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

funktioniert nicht, wenn klicken, um Dateien zu wählen Taste nicht geöffnet Galerie bitte helfen Sie mir – zain

Verwandte Themen