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

相关内容

热门资讯

专业辅助(微扑克)辅助插件挂(... 专业辅助(微扑克)辅助插件挂(辅助挂)职业代打(原来有挂)1、超多福利:超高返利,海量正版游戏,系统...
攻略辅助(微扑克)ai辅助神器... 攻略辅助(微扑克)ai辅助神器(辅助挂)ai机器人(果然存在有挂);致您一封信;亲爱玩家:《透明挂》...
透视最新(wpK)透视挂会被封... 透视最新(wpK)透视挂会被封号吗(黑科技)透视辅助有病毒(果然是有挂)1、ai辅助优化,发牌逻辑科...
黑科技辅助挂(WepOke)有... 黑科技辅助挂(WepOke)有没有挂(黑科技)到底有挂(竟然真的是有挂)是一款可以让一直输的玩家,快...
透视智能ai(wpK)外挂第一... 透视智能ai(wpK)外挂第一视角(黑科技)外挂第一视角(好像有挂)1、金币登录送、破产送、升级送、...
透视app(wpK)有透视辅助... 透视app(wpK)有透视辅助吗(黑科技)透视辅助是真实存在(原来存在有挂),您好,这款游戏可以开挂...
透视有挂(wpK)透视辅助挂(... 透视有挂(wpK)透视辅助挂(黑科技)透视辅助有病毒(切实是有挂)1、构建自己的微扑克辅助插件;2、...
黑科技插件(wepOkE)辅助... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
攻略辅助(微扑克)ai防封(辅... 攻略辅助(微扑克)ai防封(辅助挂)ai会被检测到(好像是有挂)1、在ai机器人技巧中,中转单元十分...
黑科技app(wepOke)a... 黑科技app(wepOke)ai辅助怎么用(黑科技)软件太假了(其实有挂)是一款可以让一直输的玩家,...