'When I take a photo my "error" toast is displayed directly, why is the file not being created?

I'm asking a new question, if it already exists, please excuse me I searched again and again but I didn't find anything.

My code works perfectly on the API28 emulator but as soon as I run it on the API29 emulator and above, it doesn't work...

Can you help me ?

CameraUtils.java

public static File getNewFile() {
   
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "Failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;

        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");

        return mediaFile;
    }

CameraFragment.java I only show you the "btnTakePicture.setOnCLick....

btnTakePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnTakePicture.setEnabled(false);
                AnimUtils.animateBtnTakePhoto(btnTakePicture);

                File file = CameraUtils.getNewFile();

                ImageCapture.OutputFileOptions outputFileOptions =
                        new ImageCapture.OutputFileOptions.Builder(file).build();

                imageCapture.setTargetRotation(CameraUtils.getDisplayRotation(requireContext()));
                imageCapture.setFlashMode(flashMode);
                imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(requireContext()),
                        new ImageCapture.OnImageSavedCallback() {
                            @RequiresApi(api = Build.VERSION_CODES.M)
                            @Override
                            public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                                MediaScannerConnection.scanFile(requireContext(),
                                        new String[]{file.toString()}, null, null);

                                btnTakePicture.setEnabled(true);

                                switch (typeAction) {
                                    case CameraFragment.ACTION_MESSAGE:
                                        Intent intent = new Intent(requireContext(), PhotoMessageActivity.class);
                                        intent.putExtra(PhotoMessageActivity.IMG_URI, file.getPath());
                                        intent.putExtra(ChatActivity.UID, uidReceiver);
                                        startActivity(intent);
                                        break;
                                    case CameraFragment.ACTION_STATUS:
                                        startActivity(new Intent(requireContext(), AddNewStoryPhotoActivity.class)
                                        .putExtra("IMG_URI", file.getPath()));
                                        break;
                                    case CameraFragment.ACTION_UPDATE_PICTURE:
                                        CropImage.cropMiniatureFromFragment(Uri.fromFile(new File(file.getPath())),
                                                requireContext(), CameraFragment.this);
                                        break;
                                }
                            }

                            @Override
                            public void onError(@NonNull ImageCaptureException exception) {
                                btnTakePicture.setEnabled(true);
                                Toast.makeText(requireContext(), "Error taking photo", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

My code works with API 29 emulators, but as soon as my code is tested on an API29 and API30 emulator it no longer works.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source