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

相关内容

热门资讯

微扑克透明挂,wpk辅助器是真... 您好,wpk辅助器是真是假这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
德州之星辅助器多少钱,wepo... 德州之星辅助器多少钱,wepoke怎么看有没有外挂,智星菠萝德州辅助是一款可以让一直输的玩家,快速成...
aapoker透明,wpkai... aapoker透明,wpkai辅助,智星德州菠萝辅助;无聊就玩这款wpkai辅助真的有辅助,无限炸弹...
德州之星辅助软件介绍,wpk数... 德州之星辅助软件介绍,wpk数据统计软件,智星菠萝德州辅助是一款可以让一直输的玩家,快速成为一个“必...
智星德州菠萝怎么开挂,wpk辅... 智星德州菠萝怎么开挂,wpk辅助,智星菠萝德州有挂吗wpk辅助辅助器中分为三种模型:wpk辅助软件透...
wepoke用模拟器,微扑克辅... wepoke用模拟器,微扑克辅助手机,智星德州辅助器微扑克辅助手机辅助器中分为三种模型:微扑克辅助手...
wepoke透明挂要多少钱,W... wepoke透明挂要多少钱,WePoKe有挂吗,智星菠萝德州辅助wepoke透明挂要多少钱辅助器中分...
智星德州菠萝开挂,wepoke... 智星德州菠萝开挂,wepoke辅助挂,智星菠萝德州有挂吗;小薇(透视辅助)致您一封信;亲爱wepok...
wpk透视外挂购买渠道,wpk... wpk透视外挂购买渠道,wpk ai机器人,苹果菠萝德州辅助神器是一款可以让一直输的玩家,快速成为一...
德州之星app辅助器,wepo... 德州之星app辅助器,wepokeai代打,智星德州菠萝辅助器免费下载1、许多玩家不知道wepoke...