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

相关内容

热门资讯

wepower有外挂!微扑克后... wepower有外挂!微扑克后台能控制胜率吗,(Wepoke)原来真的是有挂(详细辅助透明教程);大...
透视代打(德州AI智能辅助机器... 透视代打(德州AI智能辅助机器人)wepoke有插件吗(详细辅助透明教程)其实是有挂;致您一封信;亲...
wpk有外挂!wepoke德扑... wpk有外挂!wepoke德扑之星,(德州之星)确实存在有挂(详细透视高科技教程);(需添加指定Q群...
透视科技(德州ai辅助有用)微... 透视科技(德州ai辅助有用)微扑克辅助机器人(详细辅助软件教程)都是真的有挂是一款可以让一直输的玩家...
wepoke真的有挂!wepo... wepoke真的有挂!wepoke的机制,(wEpOke)原生存在有挂(详细辅助微扑克教程)准备好在...
透视肯定(aapoker辅助工... 透视肯定(aapoker辅助工具存在)微扑克怎么用ai辅助(详细辅助扑克教程)素来是真的有挂(1)透...
微扑克游戏辅助器!wepoke... 微扑克游戏辅助器!wepoke德州扑克系统规律,(wepOke)总是是有挂(详细透视AI教程);人气...
透视了解(德州AI智能辅助机器... 透视了解(德州AI智能辅助机器人)wpk发牌(详细辅助解密教程)其实真的有挂;辅助插件是一个在免局卡...
透视app(微扑克辅助机器人)... 透视app(微扑克辅助机器人)德扑软件决策(详细辅助新2025版)先前存在有挂;透视app(微扑克辅...
aapoker透明挂!德州之星... aapoker透明挂!德州之星辅助器怎么用,(德州之星)先前是有挂(详细辅助wpk教程);亲,其实确...