无法使用WebChromeClient在WebView Android 4.4.2中打开文件选择器

时间:2015-02-19 06:45:09

标签: android android-4.4-kitkat webviewclient webchromeclient android-afilechooser

在我的网页浏览中点击“选择文件”按钮在三星7英寸平板电脑中,我想打开文件浏览器,但我无法在 Android版本4.4中打开它0.2 即可。 那么你能帮助我吗? 这是我的代码。

WebViewDemo.java

package com.example.webviewdemo;

import java.io.File;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.webkit.ConsoleMessage;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WebViewDemo extends Activity {
    /** Called when the activity is first created. */

    WebView web;

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

    @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;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_web_view);

        web = (WebView) findViewById(R.id.webView1);

        web = new WebView(this);
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("file:///android_asset/select.html");
        web.setWebViewClient(new myWebClient());
        web.setWebChromeClient(new WebChromeClient()
        {
            //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/*");
                WebViewDemo.this.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("*/*");
                WebViewDemo.this.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/*");
                WebViewDemo.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), WebViewDemo.FILECHOOSER_RESULTCODE );

            }

        });


        setContentView(web);


    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }

    //flipscreen not loading again
    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
    }

// To handle "Back" key press event for WebView to go back to previous screen.
/*@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}*/
}

内部资源/ select.html

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
  <h1>PRODUCT DETAIL</h1>
  </div>

<div data-role="content">
<form method="get" action="">

   <div data-role="fieldcontain">
    <label for="name">Open File Chosser And Select File From Camera :</label>
    <input type="file" name="text" id="name" type="image/*" >
    </div>
  <div data-role="fieldcontain">
    <label for="name">Email:</label>
    <input type="text" name="text" id="name" value="" placeholder="What's Your Name?">
    </div>

    <div data-role="fieldcontain">
    <label for="search">Looking for anything?</label>
    <input type="search" name="search" id="search" value="" placeholder="Search for content"/>
  </div>

  <div data-role="fieldcontain">
    <label for="colors"><a href="http://www.androidexample.com/media/webview/webview_browser.html">Open link in webview browser.</a></label>
    <br>
    <br>
    <label for="colors"><a href="http://www.google.com/">Open link in new browser.</a></label>

 </div>

   </div>

</form>
</div>
</div>

</body>
</html>

4 个答案:

答案 0 :(得分:2)

这肯定会奏效,因为我已经尝试过了......

 public class MyWb extends Activity {
/** Called when the activity is first created. */

WebView web;
ProgressBar progressBar;

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

 @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;  
  }
  }  

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    web = (WebView) findViewById(R.id.webview01);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    web = new WebView(this);  
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
    web.setWebViewClient(new myWebClient());
    web.setWebChromeClient(new WebChromeClient()  
    {  
           //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/*");  
            MyWb.this.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("*/*");
           MyWb.this.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/*");  
               MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );

           }

    });  


    setContentView(web);  


}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }
}

//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){        
    super.onConfigurationChanged(newConfig);
}

// To handle "Back" key press event for WebView to go back to previous screen.
/*@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}*/
}

答案 1 :(得分:2)

尝试

// Work on Android 4.4.2 Zenfone 5
public void showFileChooser(ValueCallback<String[]> filePathCallback,
            String acceptType, boolean paramBoolean){
         // TODO Auto-generated method stub
}

和...

public void showFileChooser(ValueCallback<String[]> uploadFileCallback,
        FileChooserParams fileChooserParams) {
        // TODO Auto-generated method stub
}
link : http://src.chromium.org/svn/trunk/src/android_webview/java/src/org/chromium/android_webview/AwContentsClient.java

我希望这对你有帮助!

答案 2 :(得分:2)

这里的问题是,在Android 4.4中,文件输入没有实现,它永远不会通过标准的WebView界面。 我在我的项目中做的是使用CrossWalk项目使其工作正常。 将默认的WebView从标准的Android视图改为CrossWalk需要相当多的工作,但它确实很好地满足了我的需求。

基本上,您需要使用CrossWalk webView替换普通的Android WebView。这对我们来说几乎没有什么好处,包括Chrome甚至可以在Android 4.0,4.1等版本上更新,从而为我们提供更好的支持和更少的错误。

我在此处记录了我的实施方式:http://blog.maurizionapoleoni.com/blog/implementing-a-file-input-with-camera-support-in-android-with-crosswalk/

答案 3 :(得分:0)

此代码在我这边运行良好。

APP_COLOR

希望它对你有用......