0

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!

Antuan
  • 501
  • 2
  • 6
  • 18
  • Stupid me. I'm very upset at myself.. After having spent days looking, right after writing this post. the answer on a completely different topic. For people who have the same issue in the future, the problem is with your permissions: if your emulator is API23 or above as mine is, you permission for camera should be: instead of – Antuan Jul 07 '16 at 20:52
  • `photosDirectory.mkdirs();`. You should not execute that statement twice. And only if the directory does not exist. – greenapps Jul 07 '16 at 21:18

1 Answers1

1

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"/>
Antuan
  • 501
  • 2
  • 6
  • 18