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

Android WebView 不支持 H5 input type="file" 解决方法

程序员文章站 2023-12-10 20:17:34
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了h5并嵌入webview中。发现点击h5中 input type="file" 标签 不能打开android资...

最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了h5并嵌入webview中。发现点击h5中 input type="file" 标签 不能打开android资源管理器。

通过网络搜索发现是因为 android webview 由于考虑安全原因屏蔽了 input type="file" 这个功能 。

经过不懈的努力,以及google 翻译的帮助 在 * 中找到了解决的方法。

具体可以理解为 重写webview 的webchromeclient ,废话不多说直接贴代码:

private valuecallback<uri> muploadmessage;
public valuecallback<uri[]> uploadmessage;
public static final int request_select_file = 100;
private final static int filechooser_resultcode = 2;
webview.setwebchromeclient(new webchromeclient(){
    // for 3.0+ devices (start)
    // onactivityresult attached before constructor
    protected void openfilechooser(valuecallback uploadmsg, string accepttype)
    {
      muploadmessage = uploadmsg;
      intent i = new intent(intent.action_get_content);
      i.addcategory(intent.category_openable);
      i.settype("image/*");
      startactivityforresult(intent.createchooser(i, "file browser"), filechooser_resultcode);
    }
    // for lollipop 5.0+ devices
    @targetapi(build.version_codes.lollipop)
    public boolean onshowfilechooser(webview mwebview, valuecallback<uri[]> filepathcallback, webchromeclient.filechooserparams filechooserparams)
    {
      if (uploadmessage != null) {
        uploadmessage.onreceivevalue(null);
        uploadmessage = null;
      }
      uploadmessage = filepathcallback;
      intent intent = filechooserparams.createintent();
      try
      {
        startactivityforresult(intent, request_select_file);
      } catch (activitynotfoundexception e)
      {
        uploadmessage = null;
        toast.maketext(getbasecontext(), "cannot open file chooser", toast.length_long).show();
        return false;
      }
      return true;
    }
    //for android 4.1 only
    protected void openfilechooser(valuecallback<uri> uploadmsg, string accepttype, string capture)
    {
      muploadmessage = uploadmsg;
      intent intent = new intent(intent.action_get_content);
      intent.addcategory(intent.category_openable);
      intent.settype("image/*");
      startactivityforresult(intent.createchooser(intent, "file browser"), filechooser_resultcode);
    }
    protected void openfilechooser(valuecallback<uri> uploadmsg)
    {
      muploadmessage = uploadmsg;
      intent i = new intent(intent.action_get_content);
      i.addcategory(intent.category_openable);
      i.settype("image/*");
      startactivityforresult(intent.createchooser(i, "file chooser"), filechooser_resultcode);
    }
  });
}
@override
public void onactivityresult(int requestcode, int resultcode, intent intent)
{
  if(build.version.sdk_int >= build.version_codes.lollipop)
  {
    if (requestcode == request_select_file)
    {
      if (uploadmessage == null)
        return;
      uploadmessage.onreceivevalue(webchromeclient.filechooserparams.parseresult(resultcode, intent));
      uploadmessage = null;
    }
  }
  else if (requestcode == filechooser_resultcode)
  {
    if (null == muploadmessage)
      return;
    // use mainactivity.result_ok if you're implementing webview inside fragment
    // use result_ok only if you're implementing webview inside an activity
    uri result = intent == null || resultcode != mainactivity.result_ok ? null : intent.getdata();
    muploadmessage.onreceivevalue(result);
    muploadmessage = null;
  }
  else
    toast.maketext(getbasecontext(), "failed to upload image", toast.length_long).show();
}

以上所述是小编给大家介绍的android webview 不支持 h5 input type="file" 解决方法,希望对大家有所帮助