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。默认情况下,照片应保存在大于或等于最低相机分辨率的分辨率下。这基本上意味着照片的尺寸可能小于设备屏幕的尺寸,但可以根据需要放大。

相关内容

热门资讯

原来有透视"wepo... 原来有透视"wepoker透视挂教程"开挂(透视)辅助脚本(重大消息实用技巧)原来有透视"wepok...
正品透视"wepok... 正品透视"wepoker开挂辅助挂教程"开挂(透视)辅助挂(玩家必看科普规律教程)正品透视"wepo...
曝光透视"WePok... 曝光透视"WePoker辅助挂教程"开挂(透视)辅助挂(终于懂了解密教程)《曝光透视"WePoker...
原来有透视"wepo... 原来有透视"wepokerplus有没有挂"开挂(透视)辅助神器(详细说明解密教程)原来有透视"we...
正品透视"WePok... 正品透视"WePoker德州开挂神器透视"开挂(透视)辅助平台(一分钟了解科技教程)正品透视"WeP...
科技透视"wepok... 科技透视"wepoker真的有透视挂么"开挂(透视)辅助工具(免费测试版安装教程)科技透视"wepo...
实测透视"微扑克发牌... 实测透视"微扑克发牌真的很假"开挂(透视)辅助插件(实操分享透明教程)实测透视"微扑克发牌真的很假"...
传授透视"WePok... 传授透视"WePoker透视插件怎么安装"开挂(透视)辅助器(2026教程必胜教程)传授透视"WeP...
实测透视"wepok... 实测透视"wepoker有挂吗开挂"开挂(透视)辅助插件(一分钟了解专业教程)实测透视"wepoke...
科技透视"wepok... 科技透视"wepokerplus透视挂真的假的"开挂(透视)辅助工具(必知教程可靠教程)【福星临门,...