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();
    }
相关推荐
ueotek8 小时前
Ansys Zemax | 手机镜头设计 - 第 4 部分:用 LS-DYNA 进行冲击性能分析
智能手机·ansys·zemax·光学·手机镜头设计·光学系统
羊仔AI探索10 小时前
3步在小米13手机跑DeepSeek R1
人工智能·智能手机
gfgfgg0011 天前
谷歌地图免费下载手机版
智能手机
开开心心_Every2 天前
免费且好用的PDF水印添加工具
android·javascript·windows·智能手机·pdf·c#·娱乐
东风西巷2 天前
无他相机:专业摄影,触手可及
数码相机·智能手机·软件需求
getapi2 天前
flutter开发安卓APP适配不同尺寸的手机屏幕
android·flutter·智能手机
非凡ghost3 天前
ChatOn:智能AI聊天助手,开启高效互动新时代
android·人工智能·智能手机·生活·软件需求
东风西巷3 天前
OldRoll复古胶片相机:穿越时光,定格经典
数码相机·智能手机·软件需求
东风西巷4 天前
途景VR智拍APP:开启沉浸式VR拍摄体验
智能手机·vr·软件需求
开开心心_Every4 天前
功能丰富的PDF处理免费软件推荐
运维·笔记·qt·智能手机·pdf·音视频·ruby