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万能辅助器(透视)透视脚本下载(原来真的有挂)该软件可以轻松地帮助玩家将aa...
透视最新!aapoker怎么选... 透视最新!aapoker怎么选牌(透视)ai插件(竟然存在有挂)1、aapoker怎么选牌系统规律教...
透视辅助!aapoker怎么开... 透视辅助!aapoker怎么开辅助器(透视)辅助(好像是真的有挂)1、进入到aapoker怎么开辅助...
透视安卓版!aapoker万能... 透视安卓版!aapoker万能辅助器(透视)脚本(果然有挂)1、超多福利:超高返利,海量正版游戏,a...
透视中牌率!aapoker发牌... 透视中牌率!aapoker发牌逻辑(透视)插件下载(真是有挂);1、超多福利:超高返利,海量正版游戏...
透视规律!aapoker辅助软... 透视规律!aapoker辅助软件合法吗(透视)透视脚本入口(总是真的有挂)aapoker辅助软件合法...
透视规律!aapoker辅助器... 透视规律!aapoker辅助器是真的吗(透视)透视方法(真是真的有挂)aapoker辅助器是真的吗软...
透视能赢!aapoker俱乐部... 透视能赢!aapoker俱乐部靠谱吗(透视)透视脚本(确实真的有挂);1、玩家可以在aapoker俱...
透视讲解!aapoker辅助器... 透视讲解!aapoker辅助器是真的吗(透视)免费透视脚本(真是有挂);1、打开软件启动之后找到中间...
透视好友!aapoker怎么选... 透视好友!aapoker怎么选牌(透视)透视软件(本来是真的有挂)1、起透看视 aapoker怎么选...