• WebView内置方案主要是通过重写WebChromeClient 来实现的,如下面的代码所示。


    基本思想也很简单:通过WebChromeClient的方法以startActivityForResult的方式打开系统的文件选择器,选择文件后在onActivityResult中将结果回传给Webview即可。

    当你的App最低支持版本为Android5.0及以上就很简单了,只要重写WebChromeClient 中的 onShowFileChooser()的方法即可。但是如果是5.0以下,那么还需要提供几个适配的方法,如下面的代码所示。

    public class WebviewFileChooserAct extends AppCompatActivity{
    private static final int REQUEST_CODE_FILE_CHOOSER = 1;

    private ValueCallback<Uri> mUploadCallbackForLowApi;
    private ValueCallback<Uri[]> mUploadCallbackForHighApi;

    private WebView mWebView;
    private WebChromeClient myWebChromeClient = new WebChromeClient() {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
    mUploadCallbackForHighApi = filePathCallback;
    Intent intent = fileChooserParams.createIntent();
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    try {
    startActivityForResult(intent, REQUEST_CODE_FILE_CHOOSER);
    } catch (ActivityNotFoundException e) {
    mUploadCallbackForHighApi = null;
    Toast.makeText(EShopAiCustomServiceAct.this, R.string.cant_open_file_chooser, Toast.LENGTH_LONG).show();
    return false;
    }
    return true;
    }

    // For 3.0+
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
    openFilerChooser(uploadMsg);
    }

    //For Android 4.1+
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
    openFilerChooser(uploadMsg);
    }
    };

    private WebViewClient myWebViewClient = new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    super.onReceivedError(view, errorCode, description, failingUrl);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    return;
    }
    //Web页面加载失败
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    super.onReceivedError(view, request, error);
    if (request.isForMainFrame()) {
    //Web页面加载失败
    }
    }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_market_ai_custom_service);
    mWebView = findViewById(R.id.webview);
    configWebView(mWebView);
    mWebView.loadUrl("you webpage url");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_FILE_CHOOSER && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
    afterFileChooseGoing(resultCode, data);
    }
    }

    private void afterFileChooseGoing(int resultCode, Intent data) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    if (mUploadCallbackForHighApi == null) {
    return;
    }
    mUploadCallbackForHighApi.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
    mUploadCallbackForHighApi = null;
    } else {
    if (mUploadCallbackForLowApi == null) {
    return;
    }
    Uri result = data == null ? null : data.getData();
    mUploadCallbackForLowApi.onReceiveValue(result);
    mUploadCallbackForLowApi = null;
    }
    }

    private void configWebView(WebView webView) {
    WebSettings settings = webView.getSettings();
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setAllowFileAccess(true);
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);
    webView.setWebViewClient(myWebViewClient);
    webView.setWebChromeClient(myWebChromeClient);
    }

    private void openFilerChooser(ValueCallback<Uri> uploadMsg) {
    mUploadCallbackForLowApi = uploadMsg;
    startActivityForResult(Intent.createChooser(getFilerChooserIntent(), "File Chooser"), REQUEST_CODE_FILE_CHOOSER);
    }

    private Intent getFilerChooserIntent(http://www.my516.com) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    return intent;
    }
    }

  • 相关阅读:
    性能测试知多少吞吐量
    性能测试知多少性能测试工具原理与架构
    性能测试知多少了解前端性能
    性能测试知多少响应时间
    性能测试知多少性能测试流程
    LoadRunner脚本编写之三(事务函数)
    测试人员的家在哪儿
    oracle的启动过程
    Oracle表空间(tablespaces)
    LoadRunner脚本编写之二
  • 原文地址:https://www.cnblogs.com/ly570/p/11460292.html
Copyright © 2020-2023  润新知