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

相关内容

热门资讯

黑科技了解!德扑ai智能机器人... 黑科技了解!德扑ai智能机器人线上代打(ai辅助)竟然真的是有挂(有挂插件)-哔哩哔哩;支持多人共享...
黑科技私人局!gg扑克正规吗(... 黑科技私人局!gg扑克正规吗(智能ai)太坑了存在有挂(AI教程黑科技脚本)-哔哩哔哩1、gg扑克正...
黑科技安卓版!德扑之星如何开房... 黑科技安卓版!德扑之星如何开房间(黑科技)太坑了有挂(安装教程黑科技方法)-哔哩哔哩1、在德扑之星如...
黑科技讲解!wepoke有软件... 黑科技讲解!wepoke有软件吗(透视)原先是有挂(有挂插件挂)-哔哩哔哩;最新版2024是一款经典...
黑科技透明!红龙扑克是正规的吗... 黑科技透明!红龙扑克是正规的吗(黑科技ai)太坑了真的是有挂(德州教程黑科技教程)-哔哩哔哩1、许多...
黑科技了解!德扑ai智能机器人... 1、黑科技了解!德扑ai智能机器人代理(透视)确实真的是有挂(有挂安装)-哔哩哔哩(UU poker...
黑科技安卓版!wopoker手... 黑科技安卓版!wopoker手机辅助(ai代打)太坑了真的有挂(AI教程黑科技工具)-哔哩哔哩1、完...
黑科技总结!aa poker智... 黑科技总结!aa poker智能ai辅助介绍(黑科技ai)切实存在有挂(有挂安装)-哔哩哔哩关于aa...
黑科技安装!aapoker有后... 黑科技安装!aapoker有后台操控吗(智能ai)太坑了真的有挂(黑科技教程黑科技辅助)-哔哩哔哩;...
黑科技ai代打!苹果菠萝德州辅... 黑科技ai代打!苹果菠萝德州辅助神器(智能ai)原生是有挂(有挂测试)-哔哩哔哩;最新版2024是一...