3

I am trying to take image and save it in a folder using camera intent. The code is working very fine in other Android versions (upto Android 10). But the same code is not running on Android 11. I am also recording video using Custom video camera, but it is working fine in Android 11 also. Please help me with the same.

Camera code :-

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                                // Ensure that there's a camera activity to handle the intent
                                                if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                                                    // Create the File where the photo should go
                                                    File photoFile = null;
                                                    try {
                                                        photoFile = Utilites.createImageFile(photocount, getActivity());
                                                        
                                                    } catch (IOException ex) {
                                                        // Error occurred while creating the File
                                                        Toast.makeText(getActivity(), "Catched No Directory",
                                                                Toast.LENGTH_SHORT).show();
                                                    }
                                                    // Continue only if the File was successfully created
                                                    if (photoFile != null) {
                                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                                            takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                                                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                                                            startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
                                                        } else {
                                                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                                                    Uri.fromFile(photoFile));
                                                            startActivityForResult(takePictureIntent, CAMERA_CAPTURE);
                                                        }
                                                    } else {
                                                        Toast.makeText(getActivity(), "No Directry", Toast.LENGTH_SHORT).show();
                                                    }
                                                } else {
                                                    Toast.makeText(getActivity(), "Take pic intent null", Toast.LENGTH_SHORT).show();
                                                }
                                                
                                            

The Utility class code where the image file is created. The folder name for saving images is based on the number generated from server.

public static File createImageFile(int id, Activity activity) throws IOException {
        String mCurrentPhotoPath;
        // Create an image file name
        String imageFileName = "";       
        File storageDir = new File(activity.getFilesDir() + "/Photos/" + Job_Inspections_Activity.LeadID + "/Pictures/");
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

        String directory = storageDir.toString();
        for (int i = 1; i <= id; i++) {
            Long tsLong = System.currentTimeMillis() / 1000;
            String ts = tsLong.toString();
            File file = new File(directory + "/" + ts + "-" + i + "~Photo.jpg");
            if (file.exists()) {
                // do nothing
                // as the image already exists and if we do anything it might override it
                //so leave this condition as it is.
            } else {
                // if the given file name is not found then pass the first name that is encountered and do not exists.
                // and then break the loop as it useless to continue to do so.
                imageFileName = ts + "-" + i + "~Photo.jpg";
                break;
            }
        }

        File image = new File(storageDir, imageFileName);
        /*File.createTempFile(
                imageFileName,  *//* prefix *//*
                ".jpg",         *//* suffix *//*
                storageDir      *//* directory *//*
        );*/

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

I have checked in device explorer of the emulator, it is showing the files created, using above method, but camera always returns 0 (zero) in result code.

My module level build.gradle file info

compileSdkVersion 29
buildToolsVersion "28.0.3"

minSdkVersion 19
targetSdkVersion 29

I am also not getting any warning/error messages in Logcat.

I have tried many solutions for this problem, but none of them worked.

BhushanDC
  • 73
  • 6
  • 3
    You should have been crashing for years with a `FileUriExposedException`, as `Uri.forFile()` has effectively been banned since Android 7.0. You may have some hack code to disable that exception via `StrictMode`. If so, please remove it. Then, switch to `FileProvider` and its `getUriForFile()` method to get your `Uri`. See [this older sample app](https://github.com/commonsguy/cw-omnibus/tree/v9.0/Camera/FileProvider) for how to use `FileProvider` with `ACTION_IMAGE_CAPTURE`. – CommonsWare Dec 27 '20 at 12:23
  • @CommonsWare, Yes Sir, you are right about the StrictMode. But the reason I was using this is that the folder name is generated dynamically, as per the number recieved from server. Meanwhile I implement FILEPROVIDER, as suggested by you, can you please tell me if I can use FILEPROVIDER for dynamic folder name ? The path is created as "/com.myapp.package/Photos/121212/Pictures". The number in the middle of the path is recieved from server. – BhushanDC Dec 28 '20 at 09:45
  • 2
    You would configure `FileProvider` to serve `Photos/`, which will include all subdirectories with all numbers. – CommonsWare Dec 28 '20 at 12:23
  • Thank You @CommonsWare Sir. The issue got resolved. If anyone gets stuck due to this issue, plese refer the solution in the comment. – BhushanDC Jan 24 '21 at 06:49

0 Answers0