首页 > 其他分享 >Android的webview区分录相还是拍照

Android的webview区分录相还是拍照

时间:2023-10-30 11:57:54浏览次数:41  
标签:webSettings 录相 void public webView Override webview null Android

背景:集成了一个实名认证的网页,iOS正常但Android点击没有反应,首页想着是webview的某些协议没有实现,于是在网上找到了相关资料,我们简单记录一下核心代码。

1.权限配置

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  

2.初始化页面

public class WebViewActivity extends BaseActivity {
    private PaxWebChromeClient chromeClient;
    private String mUrl;
    private WebView webView;

    public static void start(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView(View rootView) {
        webView = (WebView) findViewById(R.id.webview);
        Intent intent = getIntent();
        mUrl = intent.getStringExtra("url");
        chromeClient = new PaxWebChromeClient(this);

        initWebViewSettings();

        webView.setWebChromeClient(chromeClient);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl(mUrl);
        initChildInfo();
    }

    private void initWebViewSettings() {
        if (webView == null) {
            throw new NullPointerException("webview is null. please use initWebView(WebView webView) method");
        }

        webView.requestFocusFromTouch();// 支持获取手势焦点,输入用户名、密码或其他

        String ua = webView.getSettings().getUserAgentString();
        webView.getSettings().setUserAgentString(ua+ "zdgj_1.0.0");

        WebSettings webSettings = webView.getSettings();
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        webSettings.setJavaScriptEnabled(true);//支持js
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口

        webSettings.setDatabaseEnabled(false);// 数据库缓存
        webSettings.setAppCacheEnabled(false);// 打开缓存
        webSettings.setDomStorageEnabled(true);// 打开dom storage缓存
        webSettings.setAllowFileAccess(true);  //设置可以访问文件
        webSettings.setAllowContentAccess(true); // 是否可访问Content Provider的资源,默认值 true
        webSettings.setAllowFileAccess(true);    // 是否可访问本地文件,默认值 true
        webSettings.setJavaScriptEnabled(true);// 允许网页与JS交互
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);// 提高渲染级别
        webSettings.setLoadsImagesAutomatically(true);// 自动加载网络图片
        webSettings.setDefaultTextEncodingName("utf-8");// 设置默认编码方式
        // 是否允许通过file url加载的Javascript读取本地文件,默认值 false
        webSettings.setAllowFileAccessFromFileURLs(false);
        // 是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https),默认值 false
        webSettings.setAllowUniversalAccessFromFileURLs(false);
        // 支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);// 布局方式
    }

    protected void initChildInfo() {
        webView.addJavascriptInterface(new onJavaScriptInterface(), "android");
    }

    private class onJavaScriptInterface {
        /**
         * 返回上个界面
         */
        @JavascriptInterface
        public void finishView() {
            webView.post(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        chromeClient.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 处理返回键,在webview界面,按下返回键,不退出程序
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();
                return true;
            } else {
                onActivityBackPressed();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            webView.stopLoading();
            webView.getSettings().setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.removeAllViews();
            webView.destroy();
        } catch (Exception e) {
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            hideStatusView();
        }
    }
}

  

3.核心区分拍照还是录相

public class PaxWebChromeClient extends WebChromeClient {
    private static final String TAG = "PaxWebChromeClient";
    private final static int VIDEO_REQUEST = 0x11;
    private final static int PHOTO_REQUEST = 0x22;
    private Activity mActivity;
    private ValueCallback<Uri> uploadFile;//定义接受返回值
    private ValueCallback<Uri[]> uploadFiles;
    private Uri imageUri;

    public PaxWebChromeClient(@NonNull Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onPermissionRequest(PermissionRequest request) {
        request.grant(request.getResources());
    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        this.uploadFile = uploadMsg;
        openCamera(acceptType);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsgs) {
        this.uploadFile = uploadMsgs;
    }

    // For Android  > 4.1.1
   //@Override
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        this.uploadFile = uploadMsg;
        openCamera(acceptType);
    }

    // For Android  >= 5.0
    @Override
    @SuppressLint("NewApi")
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        this.uploadFiles = filePathCallback;
        String[] acceptTypes = fileChooserParams.getAcceptTypes();
        for (int i = 0; i < acceptTypes.length; i++) {
            LogUtils.e("类型类型类型:" + acceptTypes[i]);
        }
        if (acceptTypes.length > 0) {
            openCamera(acceptTypes[0]);
        } else {
            LogUtils.e("类型为空");
        }
        return true;
    }

    private void openCamera(String accept) {
        if (accept.contains("image")) {//            image/*
//            File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" + SystemClock.currentThreadTimeMillis() + ".jpg");
//            imageUri = Uri.fromFile(fileUri);
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                imageUri = FileProvider.getUriForFile(mFragment.getActivity(), "com.mgyb.suning" + ".fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri
//            }
//            PhotoUtils.takePicture(mFragment.getActivity(), imageUri, PHOTO_REQUEST);
            // 指定拍照存储位置的方式调起相机
            String filePath = Environment.getExternalStorageDirectory() + File.separator
                    + Environment.DIRECTORY_PICTURES + File.separator;
            String fileName = "IMG_" + DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
            imageUri = Uri.fromFile(new File(filePath + fileName));
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            mActivity.startActivityForResult(intent, PHOTO_REQUEST);
        } else if (accept.contains("video")) {//     video/*
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            // set the video file name
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.5);
            //限制时长
            intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5);
            //开启摄像机
            mActivity.startActivityForResult(intent, VIDEO_REQUEST);
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case VIDEO_REQUEST:
                    if (null != uploadFile) {
                        Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
                        uploadFile.onReceiveValue(result);
                        uploadFile = null;
                    }
                    if (null != uploadFiles) {
                        Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
                        uploadFiles.onReceiveValue(new Uri[]{result});
                        uploadFiles = null;
                    }
                    break;
                case PHOTO_REQUEST:
                    if (null == uploadFile && null == uploadFiles) return;
                    Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
                    if (uploadFiles != null) {
                        onActivityResultAboveL(requestCode, resultCode, data);
                    } else if (uploadFile != null) {
                        uploadFile.onReceiveValue(result);
                        uploadFile = null;
                    }
                    break;
                default:
                    break;
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            if (null != uploadFile) {
                uploadFile.onReceiveValue(null);
                uploadFile = null;
            }
            if (null != uploadFiles) {
                uploadFiles.onReceiveValue(null);
                uploadFiles = null;
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {
        if (requestCode != PHOTO_REQUEST || uploadFiles == null) {
            return;
        }
        Uri[] results = null;
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                results = new Uri[]{imageUri};
            } else {
                String dataString = data.getDataString();
                ClipData clipData = data.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)};
            }
        }
        uploadFiles.onReceiveValue(results);
        uploadFiles = null;
    }
}

  

标签:webSettings,录相,void,public,webView,Override,webview,null,Android
From: https://www.cnblogs.com/adongdev/p/17797432.html

相关文章

  • Android平台GB28181记录仪如何对接应急指挥调度系统?
    技术方案将Android平台的GB28181客户端记录仪与应急指挥调度系统对接,可以实现实时视频监控和数据传输,以协助应急指挥中心对工地的监控和调度。具体的对接方法如下:1.配置GB28181设备接入端记录仪:在Android设备上安装和配置GB28181客户端记录仪,确保其连接到智慧工地国标服务平台,实现......
  • 再次安装android studio,随手记
    1,确认java已经安装且版本复合要求且路径正确(系统或用户级)2,在国内版google上下载安装包(有墙没办法)3,安装androidstudio4,启动后安装andriodsdk5,安装gradle,很慢国内有腾讯镜像网站6,安装虚拟手机的镜像文件 ......
  • Android|FileProvider 的 authorities 重名会怎么样?
    先说结论:如果有两个或多个FileProvider的authorities重名,那么只有合并后的AndroidManifest.xml文件里,排在最前面的那个配置会生效。场景应用里有个自升级的功能,下载完apk后,通过FileProvider提供Uri进行安装。我修改了文件下载路径后,功能失效了,报错如下:java.lang.Illega......
  • Android|集成 slf4j + logback 作为日志框架
    最近在做一个AndroidAPP的日志改造时,想要满足如下需求:能够很方便地使用可变参数的方式输出日志;日志能够根据级别输出到控制台和文件;能够按照日期和文件大小进行日志文件的切割,滚动保存指定天数的日志,自动清理旧日志。基于这个需求,我搜了一下「Android日志框架」,大多网友推荐的......
  • android逆向
    工具MT管理器NP管理器命令#baksmali可以反编译dex#smali可以编译smail为dex应该是java-jarbaksmali.jardisassemble.\2740_8210372_dexfile.dex-oliu#反编译dex#批量dex2smalipython.exeC:\soft\py3util\android\smali_tools\smali_decompile_kk.py-ddex位......
  • android源码
    系统必须是ubuntu18.04https://releases.ubuntu.com/18.04/4核2线程12GBrepo管理的所有git库.git文件夹都在项目根目录的.repo/projects文件夹下同步代码sudoapt-getupdatesudoapt-getinstallreposudoapt-getinstallgit-coregnupgflexbisonbuild-essenti......
  • 超全面!23年秋招1000+道Android中大厂面试题集锦(含答案)
    前言整理这些面试题源于在微信群和几个刚入职的小伙伴们的一次讨论,很多小伙伴谈了自己的面试经历和体会,很多人最初鄙视刷题党,觉得开发技能最重要,但在短暂的面试过程中很挫败。转而去看面试题,但是网上面试题太多但又不全,查找很不方便,多是看过的又看,看十道才能看到面试的题目,极大的浪......
  • 关于 Android的一些理解
    首先是Android的框架图:    然后是4大组件      广播和内容提供者  我怎么感觉就是进程间通信呢。 ......
  • Android入门教程 | RecyclerView使用入门
    想必大家对列表的表现形式已经不再陌生。手机上有联系人列表,文件列表,短信列表等等。本文讲述的是在Android开发中用RecyclerView来实现列表效果。使用步骤引入RecyclerView在app的build.gradle文件中添加引用。我们使用的是androidx包。gradle:dependencies{//........
  • Android Studio中手机没有Root权限
    发现手机没有root看了好多解决办法都太复杂找到了一种简单解决办法在创建手机时选中X86那一选项,选择一个X86并且结尾为GoogleApls的手机然后在这个手机里运行就能切换为最高管理员后来又产生了另一个问题模拟器无法连接15:09*daemonnotrunning;startingnowattcp:50471......