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稳赢)外挂透明挂辅助设备(ai辅助)一直存在有挂(真是有挂)-哔哩哔哩;1、操作简...
辅助黑科技(wpk大厅)外挂透... 辅助黑科技(wpk大厅)外挂透明挂辅助插件(ai代打)好像是有挂(有挂存在)-哔哩哔哩1、wpk大厅...
黑科技智能ai(wpk安装)外... 黑科技智能ai(wpk安装)外挂透明挂辅助设备(ai代打)竟然是有挂(有挂分享)-哔哩哔哩1、点击下...
黑科技科技(轰趴十三水)外挂透... 黑科技科技(轰趴十三水)外挂透明挂辅助黑科技(透视)切实有挂(有挂分享)-哔哩哔哩;1、金币登录送、...
黑科技免费(wpk安装)外挂透... 黑科技免费(wpk安装)外挂透明挂辅助器(透明挂)好像存在有挂(有挂分析)-哔哩哔哩;1、wpk安装...
黑科技智能ai(传奇扑克)外挂... 黑科技智能ai(传奇扑克)外挂透明挂辅助科技(透视)切实存在有挂(讲解有挂)-哔哩哔哩;1、传奇扑克...
黑科技教程(WPK最新版)外挂... 黑科技教程(WPK最新版)外挂透明挂辅助软件(透明挂黑科技)一贯真的有挂(有挂详细)-哔哩哔哩;1、...
黑科技讲解(Wepoke技术)... 黑科技讲解(Wepoke技术)外挂透明挂辅助设备(透明挂黑科技)本来有挂(证实有挂)-哔哩哔哩1、全...
黑科技免费(Wepoke ai... 黑科技免费(Wepoke ai代打)外挂透明挂辅助插件(智能ai)都是存在有挂(有挂总结)-哔哩哔哩...
黑科技脚本(WPK脚本)外挂透... 黑科技脚本(WPK脚本)外挂透明挂辅助科技(黑科技ai)果然真的是有挂(有挂分析)-哔哩哔哩WPK脚...