欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-03 10:28:46
android 解决webview无法上传文件的问题 android原生的webview并不支持上传文件,需要我们自己实现相应的方法。于是我把工作中的相关代码记录下来。下...

android 解决webview无法上传文件的问题

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

1。需要定义三个变量

 private valuecallback<uri[]> uploadmessageabovel;
  private final static int file_chooser_result_code = 10000;
  private valuecallback<uri> 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<uri> 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<uri> valuecallback, string accepttype, string capture) {
      uploadmessage = valuecallback;
      openimagechooseractivity();
    }

    // for android >= 5.0
    @override
    public boolean onshowfilechooser(webview webview, valuecallback<uri[]> filepathcallback, webchromeclient.filechooserparams filechooserparams) {
      uploadmessageabovel = filepathcallback;
      openimagechooseractivity();
      return true;
    }

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