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、金币...
黑科技有挂(wepoke有没有... 黑科技有挂(wepoke有没有挂)外挂透明挂辅助助手(透视)竟然真的是有挂(黑科技详情);1、wep...
aapokerai辅助!aap... aapokerai辅助!aapoker辅助透视,(德州aapoker俱乐部)一贯真的有挂,技巧教程(...
黑科技线上(aapoker有挂... 黑科技线上(aapoker有挂)外挂透明挂辅助app(透视)确实是真的有挂(黑科技介绍)1、用户打开...
wepoke到底有没有挂!we... wepoke到底有没有挂!wepoke透明挂是真的吗,(we-poker)切实存在有挂,2025新版...
黑科技模拟器(wepoke透明... 黑科技模拟器(wepoke透明真的吗)外挂黑科技辅助技巧(透视)切实存在有挂(黑科技详情);1、操作...
aa poker有外挂吗!aa... aa poker有外挂吗!aa poker有外挂吗,(德州aapoker)本来是有挂,可靠教程(黑科...
黑科技代打(wpk透视辅助工具... 黑科技代打(wpk透视辅助工具)外挂黑科技辅助器(透视)其实是真的有挂(黑科技揭秘);1、金币登录送...
wepoke黑科技功能!wep... wepoke黑科技功能!wepower透视辅助真假,(WepokE)真是是真的有挂,详细教程(黑科技...
黑科技辅助(wepower有外... 黑科技辅助(wepower有外挂)外挂黑科技辅助助手(透视)好像真的是有挂(黑科技细节)1、黑科技辅...