Android 嵌套H5 网页,图片上传无法调用手机图片以及嵌套网页中无法调用另一款APP问题记录

最近项目中用到安卓原生APP 嵌套H5网页,使用安卓原生WebView实现,主要遇到以下两种问题,特此记录:

  1. 在H5页面,返回直接回到APP中
    1. 监听安卓的返回事件就行了 
      1. @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode==KeyEvent.KEYCODE_BACK&&shejian.canGoBack()){
                shejian.goBack();
                return true;
            }else{
                onBackPressed();
            }
            return super.onKeyDown(keyCode, event);
        }
  2. 在嵌套的网页中,点击链接跳转至另一款APP中报错
    1. 好像是因为WebView 只会以Http、Https的形式加载链接,导致 liktin://类链接无法加载
    2. 处理方法:重写webView的 WebViewClient 方法,判断url是否是此类链接,具体代码如下:
      WebViewClient webViewClient = new WebViewClient() {
          public boolean shouldOverrideUrlLoading(WebView wv, String url) {
              if (url == null) return false;
              try {
                  //判断是不是打开APP的链接
                  if (url.startsWith("liktin://")) {
                      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                      startActivity(intent);
                      return true;
                  }
              } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.shejian360.com/download/zxks/androidDownload.html"));
                  startActivity(intent);
                  return true;//没有安装该app时,返回true,表示拦截自定义链接,跳转到下载页面
              }
              //处理http和https开头的url
              wv.loadUrl(url);
              return true;
          }
      };       
      //设置WebView 的WebViewClient 
      shejian.setWebViewClient(webViewClient);
  3. 嵌套的网页中,无法调起图片上传
    1. 试了网上说的几种方法,最后用此方法解决问题
      1. private ValueCallback mUploadMessage;
        public ValueCallback uploadMessage;
        public static final int REQUEST_SELECT_FILE = 100;
        private final static int FILECHOOSER_RESULTCODE = 2;
        shejian.setWebChromeClient(new WebChromeClient() {
            // For Android 3.0+
            public void openFileChooser(ValueCallback uploadMsg) {
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
                shejian.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            }
        
            public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
                openFileChooser(uploadMsg);
            }
        
            public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
                openFileChooser(uploadMsg);
            }
        
            // For Lollipop 5.0+ Devices
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public boolean onShowFileChooser(WebView mWebView, ValueCallback 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;
                    return false;
                }
                return true;
            }
        });
@Override
protected 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;
        Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
}

 

 

你可能感兴趣的:(Android,H5)