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

相关内容

热门资讯

黑科技实锤(微扑克模拟器)wp... 黑科技实锤(微扑克模拟器)wpk游戏原先是有挂!太坑了本然真的是有挂(2024已更新)(哔哩哔哩);...
黑科技免费(微扑克脚本)aa扑... WePoke高级策略深度解析‌;黑科技免费(微扑克脚本)aa扑克果然是有挂!太无语了竟然是有挂(20...
黑科技辅助(aapoker教程... 黑科技辅助(aapoker教程)aapoker本来是真的有挂!太实锤了原先是真的有挂(2024已更新...
黑科技了解(aapoker a... 黑科技了解(aapoker ai)wopoker一直存在有挂!太实锤了果然是有挂(2023已更新)(...
黑科技玄学(Wepoke新更新... 黑科技玄学(Wepoke新更新)扑克世界app总是有挂!太离谱了总是真的是有挂(2021已更新)(哔...
黑科技辅助挂(德州版Wepok... 黑科技辅助挂(德州版Wepoke)wePOKE最初存在有挂!太嚣张了原来存在有挂(2026已更新)(...
黑科技模拟器(德州竞技联盟)W... 黑科技模拟器(德州竞技联盟)WepoKe素来是真的有挂!太无语了往昔有挂(2025已更新)(哔哩哔哩...
黑科技软件(Wepoke实锤)... 黑科技软件(Wepoke实锤)wpk德州扑克素来存在有挂!太嚣张了一向有挂(2022已更新)(哔哩哔...
黑科技游戏(Wepoke俱乐部... 黑科技游戏(Wepoke俱乐部)智星德州扑克一向真的有挂!太坑了真是存在有挂(2024已更新)(哔哩...
黑科技存在(cloud辅助)咪... 【福星临门,好运相随】;黑科技存在(cloud辅助)咪咪扑克真是有挂!太实锤了一向有挂(2021已更...