2

I have already read the documentation for Android Oreo's Behavior and changes.

I know there is different procedure to create file directory for Android Oreo (API 26)

Code :

 File mediaStorageDir = null;

    if (Build.VERSION.SDK_INT >= 26) {
        mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString(), "MyDirectory");
        Log.v("HEREEEEE","YES");
    } else {
        mediaStorageDir = new File(Environment.getExternalStorageDirectory().toString()
                + File.separator + "MyDirectory");
    }

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Toast.makeText(RecordVideoActivity.this, "Failed to create directory MyDirectory.",
                    Toast.LENGTH_LONG).show();
            return null;
        }
    }

But every time i am getting toast of Failed to create directory MyDirectory. I am able to Log.v("HEREEEEE","YES"); also but don't know it's not creating directory.

Advanced help would be appreciated.!

  • WHy do you have that if Build.VERSION check? The File(File, String) constructor exists all the way back to Android 1. – Gabe Sechan Nov 10 '17 at 07:40
  • First , i was not checking that. But i was getting the error. so i have tried this way –  Nov 10 '17 at 07:42
  • @GabeSechan Can you suggest me what i should change or why this error occurring ?? –  Nov 10 '17 at 07:45
  • Most likely a permissions issue. Do you have WRITE_EXTERNAL_STORAGE? – Gabe Sechan Nov 10 '17 at 07:49
  • Yes I have already applied all necessary permissions –  Nov 10 '17 at 07:50
  • `there is different procedure to create file directory for Android Oreo (API 26)`. No. Not at all. But since Android 6 you need to ask the user at run time to confirm requested permissions. – greenapps Nov 10 '17 at 07:59
  • Yes i know that we must apply run-time permission for `API 23>=.`. I have already applied `private static final String[] VIDEO_PERMISSIONS = { Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, };` But while doing changes in code and doing undo this was removed `Manifest.permission.WRITE_EXTERNAL_STORAGE` accidentally. So there was problem. –  Nov 10 '17 at 08:02

1 Answers1

1

Post Lollipop you have to ask for permission, you can look for the answer in this post here

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

Permission result callback:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //Create your Directory here
    }
}
Gautam Chibde
  • 1,167
  • 3
  • 14
  • 27