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();
    }
相关推荐
Digitally1 小时前
如何将数据从一加手机传输到一加手机
智能手机
HinsCoder5 小时前
【技能拾遗】——手机远程控制方案(中继服务器版)
服务器·阿里云·adb·智能手机·远程控制·wireguard
TheNextByte18 小时前
如何轻松地将联想手机中的数据传输到电脑
智能手机·电脑
是毛毛吧1 天前
豆包风波后的破局者:智谱 AutoGLM 让“AI 手机”走向公共基建
人工智能·智能手机·开源·github·开源软件
L、2181 天前
Flutter + OpenHarmony + AI:打造智能本地大模型驱动的跨端应用(AI 时代新范式)
人工智能·flutter·华为·智能手机·harmonyos
limingade1 天前
技术预研-app如何通过手机开发者选项-无线调试来使用adb-shell功能
adb·智能手机·手机无线调试adb·手机adb抓取屏幕画面·无线调试抓屏·无线调试与手机adb命令
飞鹰@四海1 天前
AutoGLM 旧安卓一键变 AI 手机:安装与使用指南
android·人工智能·智能手机
言之。1 天前
豆包手机AI Agent技术深度解析
人工智能·智能手机
wanhengidc2 天前
云手机的存储空间可以灵活扩展吗?
运维·服务器·科技·智能手机·云计算
非凡ghost2 天前
JRiver Media Center(媒体管理软件)
android·学习·智能手机·媒体·软件需求