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

相关内容

热门资讯

透视推荐!aapoker真的假... 透视推荐!aapoker真的假的(透视)wepoker透视辅助,教程指引(真的有挂)-哔哩哔哩1、透...
透视解迷!xpoker辅助(透... 透视解迷!xpoker辅助(透视)哈糖大菠萝能开挂吗,教程烘培(有挂细节)-哔哩哔哩哈糖大菠萝能开挂...
透视教你!wepoker怎么发... 透视教你!wepoker怎么发冤家牌(透视)约局吧能不能开挂,教程教材(有挂教学)-哔哩哔哩1、下载...
透视关于!德普之星辅助工具如何... 透视关于!德普之星辅助工具如何设置(透视)pokemmo脚本,教程经验(有挂细节)-哔哩哔哩1、上手...
透视教你!hhpoker辅助器... 透视教你!hhpoker辅助器视频(透视)wepoker私局辅助,教程绝活(有挂透视)-哔哩哔哩1、...
透视关于!wpk模拟器(透视)... 透视关于!wpk模拟器(透视)wepoker手机版透视脚本,教程项目(有挂存在)-哔哩哔哩1、wep...
透视科普!有哪些免费的wpk透... 透视科普!有哪些免费的wpk透视码(透视)wepoker怎么获得好牌,教程手段(有挂详情)-哔哩哔哩...
透视详细!aapoker插件下... 透视详细!aapoker插件下载(透视)wepoker有什么规律,教程手册(有挂助手)-哔哩哔哩1、...
透视开挂!wepoker怎么增... 透视开挂!wepoker怎么增加运气(透视)wpk透视辅助,教程讲义(有挂细节)-哔哩哔哩1、玩家可...
透视解谜!拱趴大菠萝挂(透视)... 透视解谜!拱趴大菠萝挂(透视)wpk私人局辅助是真的吗,教程学习(有挂助手)-哔哩哔哩1、该软件可以...