AndroidregisterForActivityResult拍照后无法返回全尺寸照片
创始人
2024-10-09 18:01:50
0

要使用registerForActivityResult和ActivityResultCallback获取完整的照片,需要使用以下代码:

private static final int REQUEST_IMAGE_CAPTURE = 1;
private ActivityResultLauncher takePictureLauncher =
        registerForActivityResult(new ActivityResultContracts.TakePicture(), result -> {
            if (result) {
                // do something with the full size image saved to the selected file
            }
        });

private String currentPhotoPath;

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    currentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            takePictureLauncher.launch(photoURI);
        }
    }
}

调用dispatchTakePictureIntent()方法启动照相机,并使用ActivityResultCallback收到拍摄结果。返回的结果将为true或false,具体取决于启动的照片应用程序的操作。如果照片已成功保存并返回,则将在代码中做些事情,例如将其显示在ImageView中。在此示例中,使用createImageFile()方法创建一个临时文件并将其传递给takePictureLauncher。默认情况下,照片应保存在大于或等于最低相机分辨率的分辨率下。这基本上意味着照片的尺寸可能小于设备屏幕的尺寸,但可以根据需要放大。

相关内容

热门资讯

黑科技规律!德州nzt软件(黑... 自定义德州nzt软件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你...
黑科技线上!aapoker 辅... 黑科技线上!aapoker 辅助工具(ai代打)原来是真的有挂(有挂插件挂)-哔哩哔哩科技教程也叫必...
黑科技攻略!微扑克辅助神器(透... 黑科技攻略!微扑克辅助神器(透明挂)起初有挂(有挂新版)-哔哩哔哩科技教程也叫必备教程,这是一款功能...
黑科技美元局!来玩德州app有... 黑科技美元局!来玩德州app有挂吗(ai辅助)本来有挂(有挂游戏)-哔哩哔哩是一款可以让一直输的玩家...
黑科技辅助!德扑ai智能机器人... 黑科技辅助!德扑ai智能机器人代理(透视)原来真的有挂(有挂私人房)-哔哩哔哩;大神普及一款德州ai...
黑科技教程!wepoke ai... 1、黑科技教程!wepoke ai(透视)先前真的有挂(有挂软件)-哔哩哔哩;详细教程。2、wepo...
黑科技好友房!aapoker发... 此外,数据分析德州(aapoker发牌机制)辅助神器app还具备辅助透视行为开挂功能,通过对客户aa...
黑科技辅助!微扑克俱乐部机器人... 黑科技辅助!微扑克俱乐部机器人(黑科技ai)往昔有挂(有挂安卓版)-哔哩哔哩是一款可以让一直输的玩家...
黑科技肯定!微扑克怎么加入俱乐... 黑科技肯定!微扑克怎么加入俱乐部(黑科技ai)竟然是有挂(有挂苹果版)-哔哩哔哩1、点击下载安装,微...
黑科技游戏!pokerrrr开... 黑科技游戏!pokerrrr开挂(透视)确实是有挂(有挂代打)-哔哩哔哩;1.pokerrrr开挂 ...