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();
    }
相关推荐
jiang_bluetooth1 天前
从ellisys空口分析蓝牙耳机回连手机失败案例
智能手机·蓝牙·lmp·tws蓝牙耳机
东风西巷1 天前
BLURRR剪辑软件免费版:创意剪辑,轻松上手,打造个性视频
android·智能手机·音视频·生活·软件需求
ARM2NCWU2 天前
云手机解决方案
服务器·智能手机
Cynthia AI3 天前
射频前端模组芯片(PA)三伍微电子GSR2337 兼容替代SKY85337, RTC7646, KCT8247HE
物联网·智能手机·智能路由器·智能音箱·射频开关芯片
鸿蒙布道师3 天前
AI原生手机:三大技术阵营的终极对决与未来展望
android·人工智能·ios·华为·智能手机·ai-native·hauwei
向明天乄3 天前
uni-app,小程序中的addPhoneContact,保存联系人到手机通讯录
智能手机·uni-app
PowerBI学谦3 天前
使用 Microsoft 365 Copilot 上传手机图片,实现更高效的信息提取
microsoft·智能手机·copilot
开开心心_Every4 天前
手机隐私数据彻底删除工具:回收或弃用手机前防数据恢复
android·windows·python·搜索引擎·智能手机·pdf·音视频
AORO_BEIDOU4 天前
遨游科普:2025年,三防平板有多智能?
网络·人工智能·安全·智能手机·电脑·信息与通信
资讯分享周4 天前
CFCA受邀参加盛京银行手机银行7.0发布会
智能手机