androidcameramkdirs

mkdirs() not working, Camera doesn't go back to my MainActivity


I'm fairly new to Android and I'm trying to use the camera via an intent. I know my permissions for API23 or lower are working. My app successfully opens the camera, but when I hit OK, it doesn't go back to my MainActivity. I found online that the reason is because my code fails to create a file for the picture. So I was able to narrow it down to mkdirs(). This function always returns false. I have tried everything and still mkdirs always returns false. I have followed exactly the same code in the documentation : https://developer.android.com/guide/topics/media/camera.html#saving-media under Saving data.

Here's my code:

private static File getOutputMediaFile(int type){

----- .... I do some checks first to make sure the SD card is mounted ...------ .....

    File photosDirectory; 
    photosDirectory = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myPhotos");

    if(!photosDirectory.exists()) {

       boolean file_creation = photosDirectory.mkdirs();

        if(!photosDirectory.mkdirs()){
            //return null; --->> HERE IS WHERE IT FAILS
        }else{
            Log.d("MyCameraApp", "FILE CREATION SUCCEEDED ");
        }
    }

----- I have some extra code here, not relevant to the question  -----
}

Things I have tried: 1- Permissions. I have already included:

2- I have also tried: try{ if(!photosDirectory.exists()) {

       boolean file_creation = photosDirectory.mkdirs();

        if(!photosDirectory.mkdirs()){
            //return null; --->> HERE IS WHERE IT FAILS
        }else{
            Log.d("MyCameraApp", "FILE CREATION SUCCEEDED ");
        }
    } catch(Exception e){ show message}

3- I have also gone into the application and make sure that the App has CAMERA and STORAGE permissions.

4- I have tried different ways of calling the same code, but still.

I'm desperate. This is driving me nuts. I have spent 4 days working on this without any luck. I would immensely appreciate any input. Thank you very much!


Solution

  • The issue doesn't have to do with mkdirs(), but with my permissions. For API >=23 instead of using:

    <uses-feature android:name="android.hardware.CAMERA" />
    

    I should have used should use:

    <uses-feature android:name="android.hardware.camera2"/>