为什么不在webview中选择文件选项?

时间:2016-10-12 19:09:53

标签: android webview upload

为什么不在webview中选择文件选项?我有一个wordpress网站,我使用 contactform7 插件上传文件选项。但是,当点击选择文件时在webview中按钮,这不起作用。

MainActivity.java

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.webkit.ValueCallback;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView)findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://grocerydoorstep.com");
        myWebView.setWebViewClient(new CustomWebViewClient());
    }

    public class CustomWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
        }

    }
    public void onBackPressed() {
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是我发现有效的唯一解决方案!

WebView webview;

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;

   }
}

//下一部分

class MyWebChromeClient extends WebChromeClient {
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {

    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    Cv5appActivity.this.startActivityForResult(
            Intent.createChooser(i, "Image Browser"),
            FILECHOOSER_RESULTCODE);
   }
 }

答案 1 :(得分:0)

,也许此链接对您有用。请检查file chooser inside Webview 在上面的链接中,他们解释了如何选择文件以及如何使用相机捕获文件并从Webview获取其路径。