Detect if the Documents directory exists on Android

0

I see that some devices have the "Documents" folder and others do not, depending on the Android version

How can it be detected if the Documents folder exists and if it does not exist to create it?

It is for an app that I need to store files in the file system and that I can use in other apps.

I have the following to detect the absolute path:

File outDir;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
    outDir =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString());
} else {
    outDir = new File(Environment.getExternalStoragePublicDirectory("Documents").toString());
}

with that you get:

  

/ storage / sdcard0 / Documents

    
asked by Webserveis 05.07.2016 в 20:13
source

1 answer

1

With the comment of @Bourne I created the following

With mkdir the directories are created.

Log.d(TAG, "isDirectory(): " + outDir.isDirectory());
boolean mBool;
if ((!outDir.exists()) && (outDir.isDirectory())) {
    mBool = outDir.mkdir();
    Log.d(TAG, "Create Dir: " + outDir + "result: " + mBool);
} else {
    Log.d(TAG, "Exist: " + outDir);

}

Another way if it is required more than once to resort to creating directories

public class FileDirUtils {


    private static final String TAG = FileDirUtils.class.getSimpleName();

    public static boolean existDirectory(File dir) {
        return !((!dir.exists()) && (dir.isDirectory()));
    }

    public static boolean createDirectory(File dir) {
        boolean mBool;
        return !existDirectory(dir) && dir.mkdir();
    }
}

Its use:

if (!FileDirUtils.existDirectory(outDir)) {
    FileDirUtils.createDirectory(outDir);
} 
    
answered by 05.07.2016 / 20:35
source