舒心、快乐,比什么都重要

Android拍照程序适配

复制代码
    public void takePic(){
        String forderPath =  getExternalFilesDir("")  + "/pic";
        File file = new File(forderPath);
        if (!file.exists()){
            file.mkdirs();
        }
        String randomNum = DateTools.getCurrentData_yyyyMMddHHmmss();
        //创建File对象,用于存储拍照后的照片
        File outputImage = new File(forderPath,randomNum + "_output_image.jpg");
        currentImagPath = outputImage.getAbsolutePath();

        try {
            if (outputImage.exists()){
                outputImage.delete();
            }
            outputImage.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= 24){
            imageUri = FileProvider.getUriForFile(context,"xxxxxx.packagename",outputImage);
        }else {
            imageUri = Uri.fromFile(outputImage);
        }
        //启动相机程序
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);

        startActivityForResult(intent,TAKE_PHOTO);
    }
复制代码

以上是拍照程序

下面是处理

复制代码
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK){
//                    String mimgUri =  imageUri.toString();
//                    String path = imageUri.getPath();
                    String path  = getProviderUriPath(imageUri);
                    Uri uri = Uri.parse(path);
                    mUploadMessage.onReceiveValue(uri);
                }
                break;
    }    
}
复制代码

其中getProviderUriPath工具类可以查看我上一篇文章

posted @   Arcturis  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示