Android解决WebView无法上传文件的问题-成都快上网建站

Android解决WebView无法上传文件的问题

Android 解决WebView无法上传文件的问题

渭源网站建设公司创新互联,渭源网站设计制作,有大型网站制作公司丰富经验。已为渭源1000+提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的渭源做网站的公司定做!

Android原生的WebView并不支持上传文件,需要我们自己实现相应的方法。于是我把工作中的相关代码记录下来。下次直接拿来用就行了。一点一滴都是经验。

1。需要定义三个变量

 private ValueCallback uploadMessageAboveL;
  private final static int FILE_CHOOSER_RESULT_CODE = 10000;
  private ValueCallback uploadMessage;

2.重写Activity相关方法

  private void openImageChooserActivity() {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILE_CHOOSER_RESULT_CODE) {
      if (null == uploadMessage && null == uploadMessageAboveL) return;
      Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
      if (uploadMessageAboveL != null) {
        onActivityResultAboveL(requestCode, resultCode, data);
      } else if (uploadMessage != null) {
        uploadMessage.onReceiveValue(result);
        uploadMessage = null;
      }
    }
  }


  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
    if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
      return;
    Uri[] results = null;
    if (resultCode == Activity.RESULT_OK) {
      if (intent != null) {
        String dataString = intent.getDataString();
        ClipData clipData = intent.getClipData();
        if (clipData != null) {
          results = new Uri[clipData.getItemCount()];
          for (int i = 0; i < clipData.getItemCount(); i++) {
            ClipData.Item item = clipData.getItemAt(i);
            results[i] = item.getUri();
          }
        }
        if (dataString != null)
          results = new Uri[]{Uri.parse(dataString)};
      }
    }
    uploadMessageAboveL.onReceiveValue(results);
    uploadMessageAboveL = null;
  }

3.重写WebChromeClient中的相关方法

 // For Android < 3.0
    public void openFileChooser(ValueCallback valueCallback) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    // For Android >= 3.0
    public void openFileChooser(ValueCallback valueCallback, String acceptType) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    //For Android >= 4.1
    public void openFileChooser(ValueCallback valueCallback, String acceptType, String capture) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    // For Android >= 5.0
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
      uploadMessageAboveL = filePathCallback;
      openImageChooserActivity();
      return true;
    }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


文章题目:Android解决WebView无法上传文件的问题
标题URL:http://kswjz.com/article/jpgieg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流