安卓使用腾讯webview踩坑

参考:
图片拍照封装
https://blog.csdn.net/villa_mou/article/details/78728417
弹窗
http://blog.csdn.net/baidu_25797177/article/details/51952880
项目需求原生功能,于是把之前的h5封装到安卓的webview中,本来以为稍微封装一下就行,,,唉,都是泪

原生webview,上,完犊子,页面整个垮掉,换方案
1.xwalk
提供一个包地址,剩下的自行百度吧,一开始搜的时候感觉是相对最好的解决方案,后来应为ndk原因放弃
implementation ‘org.xwalk:xwalk_core_library:23.53.589.4’
2.腾讯webview
资源可以在官网找到
https://x5.tencent.com/docs/access.html
腾讯的文档真的是一言难尽。。。
开始吧

private WebView webView;
// 加载的地址
private String url = "http://192.168.11.22:5500"
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   try {
   		//隐藏安卓的顶部
       if (getSupportActionBar() != null) {
           getSupportActionBar().hide();
       }
       if (webView != null) {

       } else {
           initView();
       }
   } catch (Exception e) {
       Log.e(TAG, "onCreate:e ", e);
   }

}
private void initView() {
    Log.d(TAG, "initView: ");
    //非wifi情况下,主动下载x5内核
    QbSdk.setDownloadWithoutWifi(true);
    //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
    QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
        @Override
        public void onViewInitFinished(boolean arg0) {
            //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
        }

        @Override
        public void onCoreInitFinished() {

        }
    };
    //x5内核初始化接口
    QbSdk.initX5Environment(getApplicationContext(), cb);
    webView = findViewById(R.id.wv_task);
//        webView.getSettings().setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
    webView.getSettings().setBuiltInZoomControls(false); //设置内置的缩放控件。若为false,则该WebView不可缩放
    webView.getSettings().setDisplayZoomControls(true); //隐藏原生的缩放控件
    webView.getSettings().setBlockNetworkImage(false);//解决图片不显示
    webView.getSettings().setLoadsImagesAutomatically(true); //支持自动加载图片
    webView.getSettings().setDefaultTextEncodingName("utf-8");//设置编码格式
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
    String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    webView.getSettings().setAppCachePath(appCachePath);
    webView.getSettings().setAllowFileAccess(true);    // 可以读取文件缓存
    webView.getSettings().setAppCacheEnabled(true);    //开启H5(APPCache)缓存功能
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setMixedContentMode(WebSettings.LOAD_NORMAL);     // https下访问http资源
    // JS的扩展,暂时不介绍,可自行百度
    webView.addJavascriptInterface(new JsBridge(MainActivity.this), "android");
    webView.loadUrl(url);
    Log.d(TAG, "监控界面加载的url为: " + url);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
//                webView.loadUrl(url);
            return super.shouldOverrideUrlLoading(webView, s);
        }

        @Override
        public void onPageFinished(WebView webView, String s) {
            super.onPageFinished(webView, s);
        }

        @Override
        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            sslErrorHandler.proceed();// 接受所有网站的证书
//                super.onReceivedSslError(webView, sslErrorHandler, sslError);
        }
    });
    paxWebChromeClient = new PaxWebChromeClient(this);
    //监听网页的加载
    webView.setWebChromeClient(paxWebChromeClient);
}

引入的webview的包都是腾讯的

import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

manifest权限,可能有多的,懒得删了





 
 
 
 
 
 
 
 
 

布局用的webView也是腾讯的


监听返回啥的后面再说吧,先第一个坑,webview的文件上传
webview的选择图片事件会触发webview的
webView.setWebChromeClient
还有,选择图片和拍照需要两个不同的方法,这里用弹窗实现
implementation ‘com.guoqi.widget:actionsheet:1.0’

public class PaxWebChromeClient extends WebChromeClient implements ActionSheet.OnActionSheetSelected, DialogInterface.OnCancelListener {
    private static int CHOOSE_REQUEST_CODE;
    private Activity mActivity;
    private ValueCallback uploadFile;//定义接受返回值
    private ValueCallback uploadFiles;

//    拍照 or 相册
    private Boolean Flag = true;

    public PaxWebChromeClient(@NonNull MainActivity activity) {
        this.CHOOSE_REQUEST_CODE = activity.getFilechooserResultcode();
        this.mActivity = activity;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onPermissionRequest(PermissionRequest request) {
        //                super.onPermissionRequest(request);//必须要注视掉
        request.grant(request.getResources());
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        this.uploadFile = uploadFile;
        if (Flag) {
            takePhoto();
        } else {
            openFileChooseProcess();
        }
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback uploadMsgs) {
        this.uploadFile = uploadFile;
        if (Flag) {
            takePhoto();
        } else {
            openFileChooseProcess();
        }
    }

    // For Android  > 4.1.1
//    @Override
    public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        this.uploadFile = uploadFile;
        if (Flag) {
            takePhoto();
        } else {
            openFileChooseProcess();
        }
    }

    // For Android  >= 5.0
    @Override
    public boolean onShowFileChooser(WebView webView,
                                     ValueCallback filePathCallback,
                                     WebChromeClient.FileChooserParams fileChooserParams) {
                    this.uploadFiles = filePathCallback;
        ActionSheet.showSheet(mActivity,this,this);
        return true;
    }


    private void openFileChooseProcess() {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        mActivity.startActivityForResult(Intent.createChooser(i, "Choose"), CHOOSE_REQUEST_CODE);
    }

    private Uri imageUri;
    
    /**
     * 拍照
     */
    private void takePhoto() {
        File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" + SystemClock.currentThreadTimeMillis() + ".jpg");
        imageUri = Uri.fromFile(fileUri);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            String auth = mActivity.getPackageName() + ".fileprovider";
            imageUri = FileProvider.getUriForFile(mActivity, auth , fileUri);//通过FileProvider创建一个content类型的Uri
        }
        PhotoUtils.takePicture(mActivity, imageUri, CHOOSE_REQUEST_CODE);
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("myTag:requestCode===",requestCode+"====");
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CHOOSE_REQUEST_CODE) {
                if (null != uploadFile) {
                    Uri result = data == null || resultCode != Activity.RESULT_OK ? null
                            : data.getData();
                    uploadFile.onReceiveValue(result);
                    uploadFile = null;
                }
                if (null != uploadFiles) {
                    Uri result = data == null || resultCode != Activity.RESULT_OK ? null
                            : data.getData();
                    if(result == null){
                        // 一些机型无法从getData中获取uri,则需手动指定拍照后存储照片的Uri
                        uploadFiles.onReceiveValue(new Uri[]{imageUri});
                    }else {
                        uploadFiles.onReceiveValue(new Uri[]{result});
                    }
                    uploadFiles = null;
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            if (null != uploadFile) {
                uploadFile.onReceiveValue(null);
                uploadFile = null;
            }
            if (null != uploadFiles) {
                uploadFiles.onReceiveValue(null);
                uploadFiles = null;
            }
        }
    }

    @Override
    public void onClick(int whichButton) {
        switch (whichButton) {
            case ActionSheet.CHOOSE_PICTURE:
                //相册
                openFileChooseProcess();
                break;
            case ActionSheet.TAKE_PICTURE:
                //拍照
                takePhoto();
                break;
            case ActionSheet.CANCEL:
                //取消按钮取消
                Log.d("myTag", "ActionSheet.CANCEL: ");
                uploadFiles.onReceiveValue(null);
                break;
        }
    }

//    点击其他地方取消
    @Override
    public void onCancel(DialogInterface dialogInterface) {
        uploadFiles.onReceiveValue(null);
        Log.d("myTag", "onCancel: ");
    }
}

代码太多就不一一贴了,PhotoUtil在第一个链接里面有
先到这,待续

你可能感兴趣的:(踩坑,android)