H5页面调用原生的拍照和相册

<uses-permission android:name="android.hardware.camera"/>

// 申请权限
private final int GET_PERMISSION_REQUEST = 100; //权限申请自定义码
private void getPermissions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
                .PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager
                        .PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager
                        .PERMISSION_GRANTED) {
            takePhoto();
        } else {
            //不具有获取权限,需要进行权限申请
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.CAMERA}, GET_PERMISSION_REQUEST);
        }
    } else {
        takePhoto();
    }
}

// js和原生交互的入口
webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                // (1)该方法回调时说明版本API >= 21,此时将结果赋值给 mUploadCallbackAboveL,使之 != null
                mUploadCallbackAboveL = filePathCallback;
//                takePhoto();
                getPermissions();
                return true;
            }
        });

// 跳转到原生的拍照界面

private void takePhoto() {
     Intent i = new Intent(Intent.ACTION_GET_CONTENT);
     i.addCategory(Intent.CATEGORY_OPENABLE);
     i.setType("image/*");
     startActivityForResult(Intent.createChooser(i, "Image Chooser"), 100);
 }

//处理file返回的结果
 File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" +   SystemClock.currentThreadTimeMillis() + ".jpg");
   Uri imageUri = Uri.fromFile(fileUri);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100) {
            //针对5.0以上, 以下区分处理方法
            if (mUploadCallbackBelow != null) {
                chooseBelow(resultCode, data);
            } else if (mUploadCallbackAboveL != null) {
                chooseAbove(resultCode, data);
            } else {
                Toast.makeText(this, "发生错误", Toast.LENGTH_SHORT).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * Android API >= 21(Android 5.0) 版本的回调处理
     *
     * @param resultCode 选取文件或拍照的返回码
     * @param data       选取文件或拍照的返回结果
     */
    private void chooseAbove(int resultCode, Intent data) {
        if (RESULT_OK == resultCode) {
            updatePhotos();

            if (data != null) {
                // 这里是针对从文件中选图片的处理, 区别是一个返回的URI, 一个是URI[]
                Uri[] results;
                Uri uriData = data.getData();
                if (uriData != null) {
                    results = new Uri[]{uriData};
                    mUploadCallbackAboveL.onReceiveValue(results);
                } else {
                    mUploadCallbackAboveL.onReceiveValue(null);
                }
            } else {
                mUploadCallbackAboveL.onReceiveValue(new Uri[]{imageUri});
            }
        } else {
            mUploadCallbackAboveL.onReceiveValue(null);
        }
        mUploadCallbackAboveL = null;
    }

    private void updatePhotos() {
        // 该广播即使多发(即选取照片成功时也发送)也没有关系,只是唤醒系统刷新媒体文件
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(imageUri);
        sendBroadcast(intent);
    }

    /**
     * Android API < 21(Android 5.0)版本的回调处理
     *
     * @param resultCode 选取文件或拍照的返回码
     * @param data       选取文件或拍照的返回结果
     */
    private void chooseBelow(int resultCode, Intent data) {
        if (RESULT_OK == resultCode) {
            updatePhotos();

            if (data != null) {
                // 这里是针对文件路径处理
                Uri uri = data.getData();
                if (uri != null) {
                    mUploadCallbackBelow.onReceiveValue(uri);
                } else {
                    mUploadCallbackBelow.onReceiveValue(null);
                }
            } else {
                // 以指定图像存储路径的方式调起相机,成功后返回data为空
                mUploadCallbackBelow.onReceiveValue(imageUri);
            }
        } else {
            mUploadCallbackBelow.onReceiveValue(null);
        }
        mUploadCallbackBelow = null;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == GET_PERMISSION_REQUEST) {
            int size = 0;
            if (grantResults.length >= 1) {
                int writeResult = grantResults[0];
                //读写内存权限
                boolean writeGranted = writeResult == PackageManager.PERMISSION_GRANTED;//读写内存权限
                if (!writeGranted) {
                    size++;
                }
                //录音权限
                int recordPermissionResult = grantResults[1];
                boolean recordPermissionGranted = recordPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!recordPermissionGranted) {
                    size++;
                }
                //相机权限
                int cameraPermissionResult = grantResults[2];
                boolean cameraPermissionGranted = cameraPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!cameraPermissionGranted) {
                    size++;
                }
                if (size == 0) {
//                    startCamera();
                    takePhoto();
                } else {
                    Toast.makeText(this, "请到设置-权限管理中开启", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

 

安卓的WebView可以通过与原生相机和相册进行交互,从而实现调用相机和相册后将结果返回给H5。 首先,我们需要在AndroidManifest.xml文件添加相机和相册的权限: ``` <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 接下来,在我们的WebView的相关代码,我们需要实现一个WebChromeClient来处理WebView的文件选择器。在onShowFileChooser方法,我们可以获取到调用相机和相册所返回的文件路径,然后将其返回给H5页面: ``` webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { // Android 5.0+ public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mUploadCallbackAboveL != null) { mUploadCallbackAboveL.onReceiveValue(null); } mUploadCallbackAboveL = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE); } catch (ActivityNotFoundException e) { mUploadCallbackAboveL = null; return false; } return true; } // Android 4.1+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { if (mUploadMessage != null) return; mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); if (capture != null && capture.equals("camera")) { intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 设置相机拍照后保存图片的路径 File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AppName"); if (!imageStorageDir.exists()) { imageStorageDir.mkdirs(); } File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); mCameraPhotoPath = "file:" + file.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); try { startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_CHOOSER_RESULT_CODE); } catch (ActivityNotFoundException e) { mUploadMessage = null; return; } } }); ``` 在我们的Activity,我们需要重写onActivityResult方法,将照片的路径返回给H5页面: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == FILE_CHOOSER_RESULT_CODE) { if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (mUploadCallbackAboveL != null) { Uri[] results = null; if (data != null) { 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)}; } mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } } else { if (mUploadMessage != null) { Uri result = data == null ? Uri.parse(mCameraPhotoPath) : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (mUploadCallbackAboveL != null) { mUploadCallbackAboveL.onReceiveValue(null); mUploadCallbackAboveL = null; } } else { if (mUploadMessage != null) { mUploadMessage.onReceiveValue(null); mUploadMessage = null; } } } } } ``` 通过上述代码,当WebView调用相机和相册后,会将结果通过回调的方式返回给H5页面,以便在H5页面上进行处理和展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值