App拍照,照片压缩存储到app内部,同步更新手机系统相册

H5页面调用安卓相机拍照,然后在H5页面显示:

1、H5调用相机的回调:

java 复制代码
      binding.fragmentWebWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                openCamera();
                filePathCallbacks = filePathCallback;

                return true;
            }
        });

2、开始拍照,图片存储到手机app的缓存目录中,你可以存到sdcard中

java 复制代码
    protected void openCamera(String currentComptUuid) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        File appDir = new File(parentPath + "/" + currentComptUuid);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        originalPicPath = file.getPath();

        mCameraUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", new File(originalPicPath));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);
        startActivityForResult(intent, 0x22);
    }

3、拍完照片,处理照片的回调信息,并采用鲁班缩框架压缩图片

java 复制代码
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != -1) { //如果取消拍照
            filePathCallbacks.onReceiveValue(null);
            filePathCallbacks = null;
            return;
        }

        switch (requestCode) {
            case 0x22:
                //图片压缩 不然会很大
                File appDir = new File(originalPicPath); //原图片地址
                if (!appDir.exists()) {
                    appDir.mkdir();
                }

                refrshAlbum(originalPicPath); //同步更新手机相册

                imageZips(this, originalPicPath, appDir.getParent());

                break;
        }
    }

    public void refrshAlbum(String path) {
        try {
            File imageFile = new File(path);
            MediaStore.Images.Media.insertImage(this.getContentResolver(),
                    path, imageFile.getName(), "description");
        } catch (Exception e) {
            LogUtils.log("refrshAlbum:" + e);
        }
    }


    protected String originalPicPath; //原始图片地址


    protected void imageZips(Context context, String photos, String targetPath) {//图片压缩
        Luban.with(this)
                .load(photos)
                .ignoreBy(600)
                .setTargetDir(targetPath) //压缩后文件目录, 如果希望替换原图就不要这行
                .filter(path -> !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif")))
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        Log.i("get_path", "开始压缩");
                    }

                    @Override
                    public void onSuccess(File file) {
                       Log.i("get_path", "文件名:" + file.getName() + "  压缩成功   路径:" + file);


                       Uri tempUri = Uri.fromFile(file);
                       filePathCallbacks.onReceiveValue(new Uri[]{tempUri}); //将照片给H5
                        filePathCallbacks = null;

                        File orinalFile = new File(originalPicPath); //删除原图片
                        if (orinalFile.exists()) {
                            orinalFile.delete();
                        }
                        // 这个不是在主线程,跟新ui ,切换到主线程
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.log("压缩失败:" + e);
                    }
                }).launch();
    }
相关推荐
清风66666621 小时前
基于51单片机的手机蓝牙控制8位LED灯亮灭设计
单片机·嵌入式硬件·智能手机·毕业设计·51单片机·课程设计
suki_lynn1 天前
2025年云手机场景适配的行业观察
智能手机·安卓
limingade2 天前
手机实时提取SIM卡打电话的信令声音-整体解决方案规划
android·智能手机·usb蓝牙·手机拦截电话通话声音
天向上2 天前
手机端的音视频界面或者图片文档界面共享给大屏
智能手机·音视频·多平台·手机投屏·多屏互动
非凡ghost2 天前
Control Center 安卓版:个性化手机控制中心
android·智能手机·生活·软件需求
wanhengidc2 天前
云手机选哪个比较好用?
服务器·网络·安全·游戏·智能手机
苏格拉真没有底2 天前
Wi-Fi 与蜂窝网络(手机网络)的核心区别,以及 Wi-Fi 技术未来的发展方向
网络·智能手机
limingade3 天前
手机实时提取SIM卡打电话的信令声音-当前现状与思考
智能手机·usb蓝牙·手机提取电话通话声音
wanhengidc3 天前
企业在使用巨椰云手机进行多开挂机功能的优点有哪些?
运维·服务器·安全·智能手机
杨骅麟(Patrick Young)4 天前
荣耀手机无法连接win11电脑,错误消息:“无法在此设备上加载驱动程序 (hn_usbccgpfilter.sys)。”解决方案
智能手机·电脑